class Person {
let testCase : String
init ( testCase: String ) {
self . testCase = testCase
}
deinit {
print ( " \( testCase) is being deinitialized " )
}
}
case 1. Allocation & Release
print ( " \n ---------- [ Case 1 ] ---------- \n " )
var obj1 : Person ? = Person ( testCase: " case1 " ) //count 1
obj1 = nil //count 0
case 2. ์ฐธ์กฐ ์นด์ดํธ ์ฆ๊ฐ
print ( " \n ---------- [ Case 2 ] ---------- \n " )
var obj2 : Person ? = Person ( testCase: " case2 " ) //count 1
var countUp = obj2 //count2
obj2 = nil //count 1
countUp = nil //count 0
/*
---------- [ Case 2 ] ----------
case2 is being deinitialized //์ฌ๋ผ์ง
*/
case 3. Collection ์ ์ํ ์ฐธ์กฐ ์นด์ดํธ
print ( " \n ---------- [ Case 3 ] ---------- \n " )
var obj3 : Person ? = Person ( testCase: " case3 " ) //count 1
var array = [ obj3, obj3] //count 3
obj3 = nil //count 2
array. remove ( at: 0 ) //count 1
array. remove ( at: 0 ) //count 0
case 4. ๊ฐํ ์ฐธ์กฐ, ์ฝํ ์ฐธ์กฐ
strong : ๊ธฐ๋ณธ๊ฐ. ๊ฐํ ์ฐธ์กฐ. Reference Count 1 ์ฆ๊ฐ
unowned : ๋ฏธ์์ ์ฐธ์กฐ. Count ์ฆ๊ฐํ์ง ์์. ์ฐธ์กฐ ๊ฐ์ฒด ํด์ ์์๋ ๊ธฐ์กด ํฌ์ธํฐ ์ฃผ์ ์ ์ง
weak : ์ฝํ ์ฐธ์กฐ. Count ์ฆ๊ฐํ์ง ์์. ์ฐธ์กฐํ๋ ๊ฐ์ฒด ํด์ ์ nil ๊ฐ์ผ๋ก ๋ณ๊ฒฝ
print ( " \n ---------- [ Case 4 ] ---------- \n " )
var strongObj4 = Person ( testCase: " case4 " ) //count 1
print ( strongObj4)
//weak var weakObj4 = Person(testCase: "case4") //weak -> count 0 // ๋ง๋ค์๋ง์ ๋ฐ๋ก ํ๊ดด nil
//print(weakObj4)
//unowned var unownedObj4 = Person(testCase: "case4") //์ค๋ฅ๋ฐ์ : count 0 -> ๋ง๋ค์๋ง์ ๋ฐ๋ก ํด์ , ํ์ง๋ง, unowned๋ ์ฐธ์กฐ๊ฐ์ฒด ํด์ ํ์ด๋ ๊ธฐ์กด ํฌ์ธํฐ์ฃผ์๋ฅด ์ฐธ์กฐํจ. ๊ทธ๋์ ์ ๊ทผ์ ํด๋น์ฃผ์๋ก ์ ๊ทผํ์๋ ์ค๋ฅ๋ฐ์. ๊ธฐ์กดํฌ์ธํฐ์ ๊ฐ์ด ์์ด์ ์ค๋ฅ.
//๋๋ฌธ์, unowned์ ์ธ๋๋ ์ ๋๋ก ํด์ ๋์ง ์์ ๋๋ง ์จ์ผํจ.
//print(unownedObj4)
unowned. weak - let , var
Q.๋ค์ 4์ค์ ์ฝ๋ ์ค ์ฌ์ฉ ๋ถ๊ฐ๋ฅํ ๊ฒ์?
unowned let unownedLet = strongObj4
unowned var unownedVar = strongObj4
weak let weakLet = strongObj4 //weak์ nil. let์ผ๋ก ํ๊ฒ๋๋ฉด ๋ณ์๊ฐ ๋ ์ ์์. nil๋ก ๋ฐ๋ ์ ์๊ธฐ ๋๋ฌธ์ ๊ผญ var ๋ฅผ ์จ์ผํจ.
weak var weakVar = strongObj4
weak์ ์ต์
๋์ด๊ธฐ ๋๋ฌธ์ ์ฌ์ฉํ ๋๋ ๊ผญ ์ต์
๋์ ๋ฒ๊ธฐ๊ณ ์ฌ์ฉ.
Q.unowned ์ weak ์ ํ์
์?
print ( " Unowned type: " , type ( of: unownedVar) )
//Unowned type: Person
print ( " Weak type: " , type ( of: weakVar) )
//Weak type: Optional<Person>
์ค์ฝํ ๋ฒ์์ ์ํ ํ ๋น ๋ฐ ํด์
class LocalScope {
func doSomething( ) { }
deinit {
print ( " LocalScope is being deinitialized " )
}
}
class ClassProperty {
func doSomething( ) { }
deinit {
print ( " ClassProperty is being deinitialized " )
}
}
class Application {
var prop = ClassProperty ( )
func allocateInstance( ) {
let local = LocalScope ( ) //count1
local. doSomething ( )
} //count 0 ๋ฉ์๋๊ฐ ๋๋ ๋.
deinit {
print ( " Application is being deinitialized " )
}
}
Q. ์๋์ ์ฝ๋ ์คํ ์ ์ถ๋ ฅ๋๋ ๋ฉ์์ง๋?
var app : Application ? = Application ( )
app? . prop. doSomething ( )
app? . allocateInstance ( )
Q. ClassProperty ๊ฐ์ฒด์ deinit ๋ฉ์๋๊ฐ ํธ์ถ๋๋ ค๋ฉด ์ด๋ป๊ฒ ํ ๊น์?
app = nil //์ ๋ฃ๊ฒ ๋๋ฉด ์๋์ผ๋ก ์ฌ๋ผ์ง.
app? . prop = nil //ํ๋กํผํฐ๋ง nil๋ก ๋ฐ๊พธ๋ ๊ฒฝ์ฐ๋ ์์.
class Person {
var pet : Dog ?
func doSomething( ) { }
deinit {
print ( " Person is being deinitialized " )
}
}
class Dog {
var owner : Person ?
func doSomething( ) { }
deinit {
print ( " Dog is being deinitialized " )
}
}
var giftbot : Person ? = Person ( ) //count1
var tory : Dog ? = Dog ( ) //count 1
giftbot? . pet = tory //Dog - count 2
tory? . owner = giftbot //Person - count 2
//์ ์๋ ๊น?
//giftbot = nil //Person - count 1
//tory = nil //Dog - count 1
//์ด๋ฏธ ์์ nil์ ์คฌ๊ธฐ ๋๋ฌธ์ ๋ nil์ ์ค ์ ์์
giftbot? . pet
Q.๋ ๊ฐ์ฒด๋ฅผ ๋ฉ๋ชจ๋ฆฌ์์ ํ ๋น ํด์ ํ๋ ค๋ฉด ์ด๋ป๊ฒ ํด์ผ ํ ๊น์?
// ์์ ์ฃผ์
giftbot? . pet = nil //() dog - count 1
tory? . owner = nil //() Person - count 1
giftbot = nil //nil //Person - count 0
tory = nil //nil //Dog - count0
//์์๋ฐ๋๋ฉด ๋ชจ๋ nil๋ก ๋์ด.
//๋ฐํ์ํ ์ฐธ์กฐ.
class Teacher {
var student : Student ?
deinit {
print ( " Teacher is being deinitialized " )
}
}
class Student {
// strong, unowned, weak
let teacher : Teacher //(strong)
// unowned let teacher: Teacher //(unowned: ์ฐธ์กฐ์นด์ดํธ ์ฆ๊ฐํ์ง ์์. )
// weak var teacher: Teacher?
init ( teacher: Teacher ) {
self . teacher = teacher
}
deinit {
print ( " Student is being deinitialized " )
}
}
var teacher : Teacher ? = Teacher ( ) // T- count 1
var student : Student ? = Student ( teacher: teacher!) // S- count 1, T - count 2.(unowned๋ก ๋ฃ์ ๊ฒฝ์ฐ count 1)
teacher? . student = student
print ( " \n ---------- [ teacher release ] ---------- \n " )
teacher? . student = nil //Unowned์ weak์ ์ผ์ ๊ฒฝ์ฐ๋ ํด๋น ์ฝ๋ ์ ์ธํ๊ณ ์๋๊ฒ (teacher = nil)๋ง์ผ๋ก๋ ํด์ ๋จ.
teacher = nil //์๋์ผ๋ก ํด์ ํ๊ณ ์ถ์ด ! // T - count 0
print ( " \n ---------- [ student release ] ---------- \n " )
student = nil //S - count 0
1) strong : ๋ช
์์ ์ผ๋ก nil ๋์
ํ์. teacher?.student = nil
2) unowned : ์๋์ผ๋ก deinit. nil ์ฒ๋ฆฌ ๋ ์์ฑ์ ์ ๊ทผํ๋ฉด ๋ฐํ์ ์๋ฌ ๋ฐ์
3) weak : ์๋์ผ๋ก deinit. nil ์ฒ๋ฆฌ ๋ ์์ฑ์ ์ ๊ทผํ๋ฉด nil ๋ฐํ
import UIKit
final class MemoryViewController : UIViewController {
final class Person {
var apartment : Apartment ?
let name : String
init ( name: String ) {
self . name = name
}
deinit {
print ( " \( name) is being deinitialized " )
}
}
final class Apartment {
var tenant : Person ?
let unit : String
init ( unit: String ) {
self . unit = unit
}
deinit {
print ( " Apartment \( unit) is being deinitialized " )
}
}
var person : Person ? = Person ( name: " James " ) //count 1
var apartment : Apartment ? = Apartment ( unit: " 3A " ) //count 1
func loadClass( ) {
person? . apartment = apartment
apartment? . tenant = person
}
deinit {
print ( " MemoryViewController is being deinitialized " )
}
}
var memoryVC : MemoryViewController ? = MemoryViewController ( )
memoryVC? . loadClass ( )
memoryVC = nil
1. ๋ฌธ์ ๊ฐ ์๋์ง ์๋์ง ํ์ธ
2. ๋ฌธ์ ๊ฐ ์๋ค๋ฉด ์ฐธ์กฐ์นด์ดํธ๊ฐ ์ด๋ป๊ฒ ๋ฐ๋๋ฉด์ ์ ํด๊ฒฐ์ด ๋์๋์ง ์ ๋ฆฌํ๊ณ
๋ฌธ์ ๊ฐ ์๋ค๋ฉด ์ด๋ค ๋ถ๋ถ์ด ๊ทธ๋ฐ์ง ์์๋ณด๊ณ ์ง์ ํด๊ฒฐํด ๋ณผ ๊ฒ