λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°
iOS 🍎/Swift

Swift16 - νƒ€μž…μΊμŠ€νŒ…

by yongmin.Lee 2021. 2. 10.

Type Casting

Type casting is a way to check the type of an instance, or to treat that instance as a different superclass or subclass from somewhere else in its own class hierarchy.

 

νƒ€μž…μΊμŠ€νŒ…?

- μΈμŠ€ν„΄μŠ€μ˜ νƒ€μž…μ„ 확인 ν•˜λŠ” μš©λ„  

- 클래슀의 μΈμŠ€ν„΄μŠ€λ₯Ό λΆ€λͺ¨ 클래슀의 νƒ€μž…μœΌλ‘œ μ‚¬μš©ν•  수 μžˆλŠ”μ§€ 확인 ν•˜λŠ” μš©λ„

- 클래슀의 μΈμŠ€ν„΄μŠ€λ₯Ό μžμ‹ ν΄λž˜μŠ€μ˜ νƒ€μž…μœΌλ‘œ μ‚¬μš©ν•  수 μžˆλŠ”μ§€ 확인 ν•˜λŠ” μš©λ„

- is, asλ₯Ό μ‚¬μš©

 

* μŠ€μœ„ν”„νŠΈμ˜ ν˜•λ³€ν™˜ != νƒ€μž…μΊμŠ€νŒ…

ex)

let intVal: Int = 7

let doubleVal : Double = Double(intVal)

>> intVal의 값을 μ‹€μˆ˜κ°’μœΌλ‘œ κ°€μ§€λŠ” μƒˆλ‘œμš΄ 값을 "생성" ν•˜λŠ” 것

νƒ€μž…μΊμŠ€νŒ…

- is μ—°μ‚°μžλ‘œ νƒ€μž…ν™•μΈμ„ ν•  λ•Œ μ‚¬μš©

- return : true or false

μ—… μΊμŠ€νŒ…

- as μ—°μ‚°μžλ₯Ό μ‚¬μš©

- λΆ€λͺ¨ν΄λž˜μŠ€μ˜ μΈμŠ€ν„΄μŠ€λ‘œ μ‚¬μš©ν•  수 μžˆλ„λ‘ μ»΄νŒŒμΌλŸ¬μ—κ²Œ νƒ€μž…μ •λ³΄ μ „ν™˜

- Any ν˜Ήμ€ AnyObject둜 νƒ€μž…μ •λ³΄ λ³€ν™˜ν•  수 μžˆλ‹€.

// UniversityStudent μΈμŠ€ν„΄μŠ€λ₯Ό μƒμ„±ν•˜μ—¬ Person ν–‰μ„Έλ₯Ό ν•  수 μžˆλ„λ‘ μ—… μΊμŠ€νŒ… 
var mike: Person = UniversityStudent() as Person //μ—… μΊμŠ€νŒ…

var jenny: Student = Student()
//var jina: UniversityStudent = Person() as UniversityStudent // 컴파일였λ₯˜

// UniversityStudent μΈμŠ€ν„΄μŠ€λ₯Ό μƒμ„±ν•˜μ—¬ Any ν–‰μ„Έλ₯Ό ν•  수 μžˆλ„λ‘ μ—… μΊμŠ€νŒ…
var jina: Any = Person() as Any // μ—… μΊμŠ€νŒ…
// as Any μƒλž΅κ°€λŠ₯ -> var jina: Any = Person() 

쑰건뢀 λ‹€μš΄ μΊμŠ€νŒ…

- as? μ—°μ‚°μžλ₯Ό μ‚¬μš©

- μžμ‹ 클래슀의 μΈμŠ€ν„΄μŠ€λ‘œ μ‚¬μš©ν•  수 μžˆλ„λ‘ μ»΄νŒŒμΌλŸ¬μ—κ²Œ μΈμŠ€ν„΄μŠ€μ˜ νƒ€μž…μ •λ³΄λ₯Ό μ „ν™˜

- μΊμŠ€νŒ…μ— μ‹€νŒ¨ν•˜λ©΄, 즉 μΊμŠ€νŒ…ν•˜λ €λŠ” νƒ€μž…μ— λΆ€ν•©ν•˜μ§€ μ•ŠλŠ” μΈμŠ€ν„΄μŠ€λΌλ©΄ nil을 λ°˜ν™˜

- 결과의 νƒ€μž…μ€ μ˜΅μ…”λ„ νƒ€μž…

var mike: Person = UniversityStudent() as Person
var jenny: Student = Student()
var jina: Any = Person() as Any

var optionalCasted: Student?

optionalCasted = mike as? UniversityStudent
optionalCasted = jenny as? UniversityStudent // nil
optionalCasted = jina as? UniversityStudent // nil
optionalCasted = jina as? Student // nil

κ°•μ œ λ‹€μš΄ μΊμŠ€νŒ… 

- as! μ—°μ‚°μž μ‚¬μš©

- μžμ‹ 클래슀의 μΈμŠ€ν„΄μŠ€λ‘œ μ‚¬μš©ν•  수 μžˆλ„λ‘ μ»΄νŒŒμΌλŸ¬μ—κ²Œ μΈμŠ€ν„΄μŠ€μ˜ νƒ€μž…μ •λ³΄λ₯Ό μ „ν™˜

- μΊμŠ€νŒ…μ— μ‹€νŒ¨ν•˜λ©΄, 즉 μΊμŠ€νŒ…ν•˜λ €λŠ” νƒ€μž…μ— λΆ€ν•©ν•˜μ§€ μ•ŠλŠ” μΈμŠ€ν„΄μŠ€λΌλ©΄ λŸ°νƒ€μž„ 였λ₯˜κ°€ λ°œμƒ

- μΊμŠ€νŒ…μ— μ„±κ³΅ν•˜λ©΄ μ˜΅μ…”λ„μ΄ μ•„λ‹Œ 일반 νƒ€μž…μ„ λ°˜ν™˜

var mike: Person = UniversityStudent() as Person
var jenny: Student = Student()
var jina: Any = Person() as Any

var forcedCasted: Student

forcedCasted = mike as! UniversityStudent
//forcedCasted = jenny as! UniversityStudent // λŸ°νƒ€μž„ 였λ₯˜
//forcedCasted = jina as! UniversityStudent // λŸ°νƒ€μž„ 였λ₯˜
//forcedCasted = jina as! Student // λŸ°νƒ€μž„ 였λ₯˜

 

μΊμŠ€νŒ…ν™œμš©

var mike: Person = UniversityStudent() as Person
var jenny: Student = Student()
var jina: Any = Person() as Any
var yagom: Person = Person()


func doSomethingWithSwitch(someone: Person) {
    switch someone {
    case is UniversityStudent:
        (someone as! UniversityStudent).goToMT()
    case is Student:
        (someone as! Student).goToSchool()
    case is Person:
        (someone as! Person).breath()
    }
}

doSomethingWithSwitch(someone: mike as Person) // 멀버쉽 νŠΈλ ˆμ΄λ‹μ„ κ°‘λ‹ˆλ‹€ 신남!
doSomethingWithSwitch(someone: mike) // 멀버쉽 νŠΈλ ˆμ΄λ‹μ„ κ°‘λ‹ˆλ‹€ 신남!
doSomethingWithSwitch(someone: jenny) // 등ꡐλ₯Ό ν•©λ‹ˆλ‹€
doSomethingWithSwitch(someone: yagom) // μˆ¨μ„ μ‰½λ‹ˆλ‹€


func doSomething(someone: Person) {
    if let universityStudent = someone as? UniversityStudent {
        universityStudent.goToMT()
    } else if let student = someone as? Student {
        student.goToSchool()
    } else if let person = someone as? Person {
        person.breath()
    }
}

doSomething(someone: mike as Person) // 멀버쉽 νŠΈλ ˆμ΄λ‹μ„ κ°‘λ‹ˆλ‹€ 신남!
doSomething(someone: mike) // 멀버쉽 νŠΈλ ˆμ΄λ‹μ„ κ°‘λ‹ˆλ‹€ 신남!
doSomething(someone: jenny) // 등ꡐλ₯Ό ν•©λ‹ˆλ‹€
doSomething(someone: yagom) // μˆ¨μ„ μ‰½λ‹ˆλ‹€

'iOS 🍎 > Swift' μΉ΄ν…Œκ³ λ¦¬μ˜ λ‹€λ₯Έ κΈ€

Swift18 - ν”„λ‘œν† μ½œ  (0) 2021.02.10
Swift17 - assert/guard  (0) 2021.02.10
Swift15 - Nil-Coalescing Operator  (0) 2021.02.03
Swift14 - Optional Chaining  (0) 2021.02.03
Swift13 - Initialization & Deinitialization  (0) 2021.02.02