Any & Any Object - ehrldyd15/Swift_Skills GitHub Wiki
Any & Any Object
1. Any & Any Object
ํ์ ์บ์คํ ์ ์ํํ ๋ ์ผ๋ฐ์ ์ผ๋ก ์์ ๊ด๊ณ์ ์๋ ํด๋์ค๋ผ๋ฆฌ๋ง ์บ์คํ ์ด ๊ฐ๋ฅํ์ง๋ง,
Any์ AnyObject ํ์ ์ ์ฌ์ฉํ ๊ฒฝ์ฐ, ์์ ๊ด๊ณ์ ์์ง ์์๋ ํ์ ์บ์คํ ์ ํ ์ ์๋ค.
1-1 Any: ๋ชจ๋ ํ์ ์ ์ ์ฅ
var nums: [Int] = []
nums.append(1) โ
nums.append(1.0) โ // Cannot convert value of type 'Double' to expected argument type 'Int'
nums.append("sodeul") โ // Cannot convert value of type 'String' to expected argument type 'Int'
nums.append(false) โ // Cannot convert value of type 'Bool' to expected argument type 'Int
๋น์ฐํ๊ฒ ์ง๋ง ๋ฐฐ์ด์ Intํ์ ์ผ๋ก ์ ์ธํ์ผ๋ฉด Intํ์ ์ด์ธ์๋ ์๋ฌ๊ฐ ๋ฐ์ํ๋ค.
var things: [Any] = []
things.append(1)
things.append(1.0)
things.append("AAA")
things.append(false)
things.append(Human.init()))
things.append({ print("I am AAA!") })
ํ์ง๋ง Any๋ก ์ ์ธํ๋ฉด ๋ชจ๋ ํ์ ์ด ๊ฐ๋ฅํ๋ค.
Any ํ์ ์ Value ํ์ (๊ตฌ์กฐ์ฒด, ์ด๊ฑฐํ), Reference ํ์ (ํด๋์ค, ํด๋ก์ )์ด๊ฑด ์๊ด ์์ด ๋ชจ๋ ์ ์ฅ์ด ๊ฐ๋ฅํ๋ค.
1-2 AnyObject: ๋ชจ๋ ํด๋์ค ํ์ ์ ์ ์ฅํ๋ค
AnyObject๋ก ์ ์ธ๋ ๊ฒฝ์ฐ, ํด๋์ค ํ์
๋ง ์ ์ฅํ ์ ์๋ค.
var things: [AnyObject] = []
things.append(1) โ // Argument type 'Int' expected to be an instance of a class
things.append(1.0) โ // Argument type 'Double' expected to be an instance of a class
things.append("sodeul") โ // Argument type 'String' expected to be an instance of a class
things.append(false) โ // Argument type 'Bool' expected to be an instance of a class
things.append(Teacher.init())) โ
things.append({ print("I am Sodeul!") }) โ // Argument type '()->()' expected to be an instance of a class
ํด๋์ค ํ์ ์ธ Teacher ์ธ์คํด์ค๋ฅผ ์ ์ธํ๊ณ ๋ ๋ชจ๋ ์๋ฌ๊ฐ ๋ฐ์ํ๋ค.
๋ค๋ง ์ ์ฅํ๋ ค๋ ํ์ ์ด ํด๋์ค์ด๋ฉด ๊ทธ๋ง์ด๋ผ
things.append(Teacher.init()))
things.append(Student.init()))
์์ฒ๋ผ Human์ด๊ณ Teacher์ด๊ณ ์๊ด์์ด ๋ชจ๋ ํด๋์ค ๊ฐ์ฒด๋ฅผ ์ ์ฅํ ์ ์๋ค.
2. Any & AnyObejct์ ํ์ ์บ์คํ
2-1 as๋ฅผ ์ด์ฉํ ํจํด ๋งค์นญ(switch)
as๋ ์
์บ์คํ
ํน์ ํจํด๋งค์นญ์ ์ฐ์ธ๋ค.
for thing in things {
switch thing {
case _ as Int:
print("Int Type!")
case _ as Double:
print("Double Type!")
case _ as String:
print("String Type!")
case _ as Human:
print("Human Type")
case _ as () -> ():
print("Closure Type")
default:
print("something else")
}
}
switch๋ฌธ์ ํ์ฉํด์ ํด๋น ํ์ ์ผ ๊ฒฝ์ฐ(์บ์คํ ์ ์ฑ๊ณตํ์๋) case๋ฌธ์ด ์คํ๋ ์ ์๋ค.
2-2 as? as!๋ฅผ ์ด์ฉํ ๋ค์ด์บ์คํ (Downcasting)
Any & AnyObject๊ฐ ๋ชจ๋ ํ์ ์ ์ ์ฅํ ์๋ ์์ง๋ง ๋จ์ ๋ ์๋ค.
var name: Any = "AAA"
์ด๋ฐ ์์ผ๋ก Any๋ก ์ ์ธ ํ๋ค๊ณ ํด๋ ๊ฒฐ๊ตญ์๋ String ํ์ ์ด๋ค. ํ์ง๋ง name์ ๋๊ณ String์ ๋ฉ์๋๋ ํ๋กํผํฐ์ ์ ๊ทผํ๋ ค๊ณ ํ๋ฉด

์๋์์ฑ์ด ๋์ค์ง ์๋๋ค. ์ฌ๊ธฐ์ ์ต์ง๋ก String์์ ์ ๊ณตํด์ฃผ๋ ํจ์๋ฅผ ์ฐ๋ ค๊ณ ํด๋

์๋ฌ๊ฐ ๋ฐ์ํ๋ค.
Any๋ AnyObject๋ ๋ฐํ์ ์์ ์ ํ์ ์ด ๊ฒฐ์ ๋๊ธฐ ๋๋ฌธ์,
์ปดํ์ผ ์์ ์ ์์ ๊ฐ์ด ํด๋น ํ์ ์ ๋ํด ์ ์ ์๋ค.
๋๋ฌธ์ ํด๋น ํ์ ์ ๋ฉค๋ฒ์๋ ์ ๊ทผํ ์ ์์
๋ฐ๋ผ์, ๋ง์ฝ Any ํ์ ์ธ name์ String์ผ๋ก ์ฌ์ฉํ๊ณ ์ถ๋ค๋ฉด,
if var name = name as? String {
name.append("123")
}
์ ์ฒ๋ผ as๋ as?๋ฅผ ํตํด, ๋ค์ด์บ์คํ ํ ํ์ ์ฌ์ฉํด์ผ ํ๋ค.
3. Any์ AnyObject๋ฅผ ๋จ์ฉํ๋ฉด ์๋๋ค.
Swift๋ ํ์ ์ ๋ฏผ๊ฐํ ์ธ์ด์ด๋ค.
Any์ AnyObject๋ ๊ทธ๋ฐ Swift์ ์ปจ์ ์ ์ด๊ธ๋๋ ํ์ ์ด๋ค.
๋ฐํ์ ์์ ์ ํ์ ์ด ๊ฒฐ์ ๋๊ธฐ ๋๋ฌธ์ ์ค๋ฅ๊ฐ ๋ฐ์ํ๋ฉด ๋ฐํ์ ์ค๋ฅ์ ๋น ์ง๊ฒ ๋๋ค.
๋ํ, ํ์ ์บ์คํ ํ๊ธฐ ์ ๊น์ง 2๋ฒ๊ณผ ๊ฐ์ด ๋งค์ฐ ์ฌ์ฉ์ด ๋ถํธํ๋ค.