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 |