본문 바로가기

Apple iOS

[Xcode / Swift] Type Casting(형변환) 관련 | is, as, as?, as! 차이점

형변환은 Instance 타입을 확인하거나, Instance superclass 혹은 subclass 취급하여 처리하기 위해 필요한 작업.

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.

출처 – Type Casting – Swift Official Document

 

is as 구별되며, is Instance 타입을 확인하는데 사용되며, as Instance 형변환 작업에 이용되는 명령어이다.

? ! 함께 쓰이면서 혼동을 일으키는 경우가 있으므로 아래와 같이 정리함. 다음 예시 코드를 이용해서 차이를 설명

 

class Creature

{

    var age :Int?

    init(age :Int?)

    {

        if let age = age

        {

            self.age = age

        }

    }

}

 

class Human : Creature

{

    var name :String?

   

    init(name :String?, age :Int?)

    {

        if let name = name

        {

            self.name = name

        }

        super.init(age: age)

    }

}

 

class Animal : Creature

{

    var weight :Int?

   

    init(weight: Int?, age :Int?)

    {

        if let weight = weight

        {

            self.weight = weight

        }

        super.init(age: age)

    }

}

 

 

var creature = [Animal(weight : 12, age: 5), Human(name : "Kim", age: 30)]

·         Creature Superclass 정의하고 Human Animal Class Subclass로써 Creature Class 상속

·         creature라는 instance 배열을 만들어, Animal 개체와 Human 개체를 각각 저장. creature 형은 Creature.

체크 – is

Bool 타입을 Return 하며, 지정된 Instance 특정 Class인지를 판별.

 고로, 다음과 같이 사용할 있다.

 

for item in creature

{

    if item is Animal

    {

        print("Current Item is Animal")

    }

    else

    {

        print("Current Item is NOT Animal")

    }

}

 

Downcasting – as? as!

상위 계층(부모 계층)에서 하위 계층(자식 계층)으로의 Downcasting. Downcast 방법에는 as?, as! 두가지가 있다. Swift 1.2 이후 부터 as Upcast 가능하기 때문에 제외한다. ? ! 수행하는 기능은 근본적으로 같지만 결과물에 차이가 있다.

·         ? : Optional 선택적

·         ! : Forced 강제적

as? 말그대로 선택적이며, Optional Value Return 해준다. Downcast 가능하지 않다면, nil값이 반환된다. Downcast 성공적일지 아닐지가 불확실하다면 as? 선택해주는 것이 현명하다. as? 의해 형변환된 Instance 그대로 출력해보면 Optional() Value 나오는 것을 있다.

creature[0] as? Animal //Animal

creature[0] as? Human //nil

 

print(creature[0] as? Animal) //Optional(__lldb_expr_46.Animal) 출력

as! 강제적으로 Downcast 한다. 또한, Downcast 성공적이지 않을 , runtime error 일으킨다. 절때 실패할 없는 Downcast에서 사용하는 것이 적당하다. as! 의해 Downcast Instance 출력 , Optional 값이 강제적으로 unwrap 것을 있다.

creature[0] as! Animal //Animal

creature[0] as! Human //runtime error

 

print(creature[0] as! Animal) //__lldb_expr_48.Animal 출력

간단하게 이야기해서, as? as! 동작(Downcast) 같다. 오류가 나지 않는 코드라면 as? as! 항상 같은 결과물을 낸다. 하지만, 위에 설명되어 있듯이 “Sure/Not Sure” 구분지어 각각 “!/?” 사용하는 것이 현명하다.

Upcasting – as as? as!

하위 계층(자식 계층)에서 상위 계층(부모 계층)으로의 UpCasting. as as? as! 모두 사용가능하다. 위에서 설명한 것과 같이 선택적, 강제적의 차이가 있다.

 

Upcasting 성공한 코드

let downcasted_creature = creature[0] as! Animal //Animal

let upcasted_downcasted_creature = downcasted_creature as Creature //Creature

 

Upcasting 실패하였을

·         as : 오류 반환

·         as? : nil 반환

·         as! : 오류 반환

let downcasted_creature = creature[0] as! Animal //Animal

let upcasted_downcasted_creature = downcasted_creature as Human //오류

let upcasted_downcasted_creature = downcasted_creature as? Human //nil

let upcasted_downcasted_creature = downcasted_creature as! Human //오류

 

참조

·         Type casting in swift : difference between is, as, as?, as! – Medium

·         What’s the difference between “as?”, “as!”, and “as”? – Stack Overflow

·         Type Casting – Swift Official Document

 

 

 

'Apple iOS' 카테고리의 다른 글

AES, SHA 암호화 5, Swift  (0) 2019.05.14
MacBook Air 모델 식별하기  (0) 2019.05.08
Apple MacBook Air Specs  (0) 2019.05.03