๐ŸŽ iOS/Swift

[Swift] Any, AnyObject, nil

Any: Swift์˜ ๋ชจ๋“  ํƒ€์ž…์„ ์ง€์นญํ•˜๋Š” ํ‚ค์›Œ๋“œ

 

var Anything: Any = 123
Anything = "์–ด๋–ค ํƒ€์ž…๋„ ์ˆ˜์šฉ ๊ฐ€๋Šฅ"
Anything = 123.12 

 

 

โ€ป ํ•˜๋‚˜์˜ ์ปฌ๋ ‰์…˜ ์•ˆ์— ์—ฌ๋Ÿฌ๊ฐ€์ง€ ๋‹ค๋ฅธ ํƒ€์ž…์˜ ๊ฐ’์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•

 

let anyCollection:[Any] = [0,"Busan",31.0]

let anyDictionary: [String:Any] = ["Temp":-5.0, "Home":"Seoul", "isFemale":true]

 

AnyObject: ๋ชจ๋“  ํด๋ž˜์Šค ํƒ€์ž…์„ ์ง€์นญํ•˜๋Š” ํ”„๋กœํ† ์ฝœ

 

- ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค

 

class AnyClass {}

var AnyObjectThing: AnyObject = SomeClass()

someAnyObject = 123.12 // ์˜ค๋ฅ˜

 

nil: ๊ฐ’์ด ์—†์Œ์„ ์˜๋ฏธํ•˜๋Š” ํ‚ค์›Œ๋“œ

 

→ optional์—์„œ ์ค‘์š”

 

AnyThing = nil // ์˜ค๋ฅ˜
AnyObjectThing = nil // ์˜ค๋ฅ˜