Focus On Develop ๐ŸคŸ๐ŸคŸ

[Swift] ARC (Automatic Reference Counting) ๋ณธ๋ฌธ

iOS [Swift]/๊ธฐ์ดˆ๋ฅผ ํƒ„ํƒ„ํžˆ!

[Swift] ARC (Automatic Reference Counting)

๋ˆ„๋ฆฌ๋‹ฌ์ดํ‹€ 2021. 1. 29. 23:29

Swift์—์„œ๋Š” ๋ฉ”๋ชจ๋ฆฌ ๊ด€๋ฆฌ๋ฅผ ์–ด๋–ป๊ฒŒ ํ• ๊นŒ์š”~?

Swift์—์„œ ๋ฉ”๋ชจ๋ฆฌ ๋ˆ„์ˆ˜๋ฅผ ๋ฐฉ์ง€ํ•˜๊ณ  ์„ฑ๋Šฅ์ €ํ•˜๋ฅผ ๋ง‰๊ธฐ์œ„ํ•ด ARC ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋Š”๋ฐ์š”!

์ด ARC์— ๋Œ€ํ•ด์„œ ํ•œ๋ฒˆ ์•Œ์•„๋ณด๋ ค๊ณ  ํ•ด์š”!


RC๋ž€ 

Reference Counting์˜ ์•ฝ์ž๋กœ ์• ํ”Œ์—์„œ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค.

<๋ฉ”๋ชจ๋ฆฌ์—์„œ ์–ด๋–ค Object๋ฅผ ์ฐธ์กฐํ•ด์„œ ์‚ฌ์šฉํ• ๋•Œ ํ•ด๋‹น Object๊ฐ€ ์ฐธ์กฐ๋˜๋Š” "๊ฐฏ์ˆ˜"๋ฅผ ์นด์šดํŒ…ํ•ด์„œ, ํ•ด๋‹น Object๋ฅผ ์ฐธ์กฐํ•˜๋Š” ์นด์šดํŠธ๊ฐ€ 0์ด๋ฉด ๋ฉ”๋ชจ๋ฆฌ์—์„œ ๊ทธ Object๋ฅผ ์ง€์šด๋‹ค> ๋ผ๋Š” ๊ธฐ๋ณธ์ ์ธ Rule์ด๋ผ๋Š” ์ „์ œ๋ฅผ ๋‘ก๋‹ˆ๋‹ค.

 

MRC๋ž€

Manual Reference Counting์˜ ์•ฝ์ž๋กœ Object-C์—์„œ ๊ฐœ๋ฐœ์ž๊ฐ€ ์ง์ ‘! ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ํ• ๋‹น/ํ•ด์ œ ํ•ด์ค˜์•ผ ํ• ๋•Œ ์“ฐ๋Š” ๋ฐฉ๋ฒ•์ด๋ผ๊ณ  ํ•˜๋Š”๋ฐ, ์ด ๋ถ€๋ถ„์€ ์ฟจํ•˜๊ฒŒ ์š”์ •๋„๋กœ ํŒจ์Šคํ•ฉ๋‹ˆ๋‹ค.ใ…Ž ์ด๋ฏธ Swift๊ฐ€ ๋ณดํŽธํ™” ๋˜์—ˆ๊ณ , ์ € ๋˜ํ•œ Object-C๋Š” 1๋„ ๋ชจ๋ฅด๊ณ  ํ˜„์žฌ ์‚ฌ์šฉ๋„ ์•ˆํ•˜๊ธฐ ๋•Œ๋ฌธ..ใ…‹๐Ÿ‘๐Ÿ‘

 

 

์ž ์ด์ œ ๋ณธ๋ก ์œผ๋กœ ๋“ค์–ด๊ฐ€์„œ,

ARC๋ž€

Automatic Reference Counting์˜ ์•ฝ์ž๋กœ ํ•œ๊ตญ์–ด๋กœ๋Š” "์ž๋™์ฐธ์กฐ๊ณ„์ˆ˜" ๋ผ๊ณ  ํ•ด์š”.

์œ„์—์„œ ์„ค๋ช…ํ•œ ๋‚ด์šฉ์œผ๋กœ ์œ ์ถ”ํ•ด๋ณด๋ฉด Automatic ์ด๋ผ๋ฉด.. ์–ด๋Š ์ •๋„ ๋Š๋‚Œ์€ ์•Œ ๊ฒƒ ๊ฐ™์ฃ ?

Swift์—์„œ๋Š” ์•Œ์•„์„œ!! ์ž๋™์œผ๋กœ!! ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ๊ด€๋ฆฌํ•ด์ค€๋‹ค๋Š” ๊ฒ๋‹ˆ๋‹ค!! RC์˜ ๊ธฐ๋ณธ์ ์ธ ์นด์šดํŒ… Rule์€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๊ฐ€์ ธ๊ฐ€๋Š”๋ฐ, ์ž๋™์œผ๋กœ RC๋ฅผ ๊ฐ์ง€ํ•˜๊ณ  ๋ฉ”๋ชจ๋ฆฌ์—์„œ ์‚ญ์ œํ•ด์ค€๋‹ค๋Š” ๊ฑฐ์ฃ  ๐Ÿ‘

 

๋ง๋กœ ๊ตฌ๊ตฌ์ ˆ์ ˆ ์„ค๋ช…ํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค "์ฝ”๋“œ+์‹œ๊ฐํ™”" ๋กœ ์ •๋ฆฌํ•ด๋†“๋Š”๊ฒŒ ํ›จ์”ฌ ๋ณด๊ธฐ ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค!!

 

๋จผ์ € Reference Count (์ฐธ์กฐ ๊ณ„์ˆ˜) ๊ฐœ๋…์— ๋Œ€ํ•ด์„œ,

class Car {
}

var a = Car() // 1๋ฒˆ ์ƒํ™ฉ
var b = a // 2๋ฒˆ ์ƒํ™ฉ

b = Car() // 3๋ฒˆ ์ƒํ™ฉ

 

1๋ฒˆ์ƒํ™ฉ ๊นŒ์ง€๋Š” a๋Š” Car ํด๋ž˜์Šค์˜ Object์ด๊ณ , assign ๋˜๋Š” ์ˆœ๊ฐ„ ๋ฉ”๋ชจ๋ฆฌ๋„ ํ• ๋‹น์ด ๋˜๊ฒ ์ฃ ?

์ด๋ ‡๊ฒŒ! ๋ฉ”๋ชจ๋ฆฌ ์–ด๋–ค ๊ณต๊ฐ„์— ์ƒ์„ฑ๋œ Object๋ฅผ a๊ฐ€ ์ฐธ์กฐํ•˜๊ฒŒ ๋˜์—ˆ๊ณ , ์ด Object๋ฅผ ์ฐธ์กฐํ•˜๋Š” ๊ฒƒ์€ ํ˜„์žฌ a ํ•˜๋‚˜ ๋ฟ์ด๋‹ˆ๊นŒ Count๋Š” 1์ด๋˜์š”.

 

2๋ฒˆ์ƒํ™ฉ์ด ๋˜๋ฉด, class๋‹ˆ๊นŒ Reference ํƒ€์ž…์œผ๋กœ b๋„ ๋™์ผํ•˜๊ฒŒ a๊ฐ€ ์ฐธ์กฐํ•˜๋Š” Object๋ฅผ ์ฐธ์กฐํ•˜๊ฒŒ ๋˜๋ฉด์„œ, ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ Object์˜ Count๋„ 2๋กœ ์ฆ๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.. a,b ๋‘˜ ๋‹ค ์ด Object๋ฅผ ์ฐธ์กฐํ•˜๋‹ˆ๊นŒ์š”!

 

3๋ฒˆ์ƒํ™ฉ์ด ๋˜๋ฉด, b๊ฐ€ ์ฐธ์กฐํ•˜๋˜ ์›๋ž˜์˜ Object๋Š” ์ฐธ์กฐํ•˜์ง€ ์•Š๊ณ  ๋ฉ”๋ชจ๋ฆฌ ๊ณต๊ฐ„์— ์ƒˆ๋กœ์šด Object๋ฅผ ์ฐธ์กฐํ•˜๊ฒŒ ๋˜์š”. ๊ทธ์— ๋”ฐ๋ผ ๊ธฐ์กด Object์˜ ์ฐธ์กฐ ์นด์šดํŠธ๋Š” 1๋กœ ์ค„์–ด๋“ค๊ณ (a๋งŒ ๋‚จ์Œ), ์ƒˆ๋กœ์šด Object ์ฐธ์กฐ ์นด์šดํŠธ๊ฐ€ 1๋กœ ์ƒ์„ฑ๋˜๊ฒ ์ฃ ?

 

์Œ.. ๊ทธ๋ž˜ ์•Œ๊ฒ ์–ด! ๊ทผ๋ฐ ์ €๊ฑด ๋„๋Œ€์ฒด ์–ธ์ œ ์นด์šดํŠธ๊ฐ€ 0์ด ๋˜์„œ ๋ฉ”๋ชจ๋ฆฌ์—์„œ ์‚ฌ๋ผ์ ธ ?????

๋‹น์—ฐํ•˜๊ฒŒ๋„! ์ฐธ์กฐํ•˜๋Š”๊ฒŒ ๋” ์ด์ƒ ์—†์„๋•Œ, ์ฆ‰ ๋” ์ด์ƒ Scope์•ˆ์—์„œ ์กด์žฌํ•˜์ง€๋„ ์•Š๊ณ  ์‚ฌ์šฉํ•  ํ•„์š”๋„ ์—†์–ด์กŒ์„๋•Œ! ์ž…๋‹ˆ๋‹ค ๐Ÿ˜‚

class Car {
}

func someMethod() {

	var a = Car()
	var b = a

	b = Car()
}

์ด๋ ‡๊ฒŒ ์œ„์™€ ๊ฐ™์€ ์ƒํ™ฉ์ด someMethod() ์•ˆ์—์„œ ๋ฐœ์ƒํ–ˆ๋‹ค๊ณ  ํ•ด๋ณด๋ฉด, someMethod๊ฐ€ ํ˜ธ์ถœ๋˜์–ด ์‹คํ–‰๋˜๋Š” ๋™์•ˆ์€ ์œ„์— 1~3๋ฒˆ ์ƒํ™ฉ๊ณผ ๋™์ผํ•˜๊ฒŒ ๋ฉ”๋ชจ๋ฆฌ์— ์กด์žฌํ•  ๊ฑฐ์—์š”. ๊ทธ๋Ÿฐ๋ฐ.. ์ € someMethod๊ฐ€ ์ข…๋ฃŒ๋˜๋ฉด!?

 

 

์ด๋ ‡๊ฒŒ ๋” ์ด์ƒ ์ด Object๋“ค์„ ์ฐธ์กฐํ•˜๋Š”๊ฒŒ ์—†์–ด์ง€๊ณ , ์‚ฌ์šฉํ•  ํ•„์š”๋„ ์—†๊ณ , Scope์—๋„ ์กด์žฌํ•˜์ง€ ์•Š์œผ๋ฉฐ ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ๋‘˜๋‹ค ์ฐธ์กฐ ์นด์šดํŠธ ์ˆ˜๊ฐ€ 0์ด ๋˜์ฃ . ๊ทธ๋Ÿฌ๋ฉด Swift์˜ Garbage Collector(๋ฉ”๋ชจ๋ฆฌ ๊ณต๊ฐ„ ์ฒญ์†Œ๊ธฐ ๊ฐ™์€๊ฑฐ ใ…‹)๋Š” ๋ฉ”๋ชจ๋ฆฌ ๊ณต๊ฐ„์—์„œ ์ด Object๋ฅผ ์ง€์›Œ๋ฒ„๋ฆฌ๋ฉด์„œ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ๊ด€๋ฆฌํ•ด์ค๋‹ˆ๋‹ค!

 

์Œ.. ๊ทธ๋Ÿฌ๋ฉด ๋ฉ”๋ชจ๋ฆฌ ๊ด€๋ฆฌ๋Š” Swift์—์„œ ์ž๋™์„ ์ฒ™์ฒ™ ํ•ด์ค€๋‹ค๋Š”๋ฐ. ๊ตณ์ด ๋‚ด๊ฐ€ ์ด ๊ฐœ๋…์„ ์•Œ์•„์•ผ ํ•  ํ•„์š”๊ฐ€ ์žˆ์–ด ??? ๊ทธ๋ ‡์Šต๋‹ˆ๋‹ค!! ๐Ÿ˜Ž

์ด ๊ฐœ๋…์„ ๋ฐ”ํƒ•์œผ๋กœ Swift์—์„œ ์•Œ์•„์„œ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ๊ด€๋ฆฌํ•ด์ฃผ์ง€๋งŒ, ์‹ค์ œ๋กœ๋Š” Swift๊ฐ€ ํ—ท๊ฐˆ๋ ค์„œ ์ฐธ์กฐ๊ฐ€ ์—†์–ด์ ธ๋ฒ„๋ฆฐ Object๋ฅผ ์—ฌ์ „ํžˆ ์‚ฌ์šฉํ•˜๋Š” ์ค„ ์ฐฉ๊ฐํ•˜๊ณ  ๋ฉ”๋ชจ๋ฆฌ์— ๋‚จ๊ฒจ๋‘๋Š” ๋ฐ”๋žŒ์—, ๋ฉ”๋ชจ๋ฆฌ ๋ˆ„์ˆ˜๊ฐ€ ์ƒ๊ธฐ๋Š” ํฌ์ธํŠธ๊ฐ€ ๋ฐœ์ƒํ•˜๊ธฐ๋„ ํ•ฉ๋‹ˆ๋‹ค.

 

์‘? ์•Œ์•„์„œํ•ด์ค€๋‹ค๋ฉฐ!?  ๋„ค.. ์ €๋„ ์†์•˜์Šต๋‹ˆ๋‹ค..ใ…Ž ์–ธ์ œ ๋ฉ”๋ชจ๋ฆฌ ๋ˆ„์ˆ˜๊ฐ€ ๋ฐœ์ƒํ•˜๊ณ , ์–ด๋–ป๊ฒŒ ๋ฐฉ์ง€ํ•ด์•ผ ํ• ์ง€ ๊ณ„์†ํ•ด์„œ ์•Œ์•„๋ด…์‹œ๋‹ค! ๐ŸคŸ

๐Ÿ‘‰  2021/01/29 - [iOS [Swift]/๊ธฐ์ดˆ๋ฅผ ํƒ„ํƒ„ํžˆ!] - [Swift] strong, weak, unowned

 

 

 

[Swift Docs] docs.swift.org/swift-book/LanguageGuide/AutomaticReferenceCounting.html

Comments