Type Casting - The Swift Programming Language (Swift 5.7)
Any
함수 타입을 포함하여 모든 타입의 인스턴스를 나타낼 수 있습니다.
var anyArray: [Any] = [1,2,"3","4"]
var anyArray1: Array<Any> = [1,true,"3",1.0]
AnyObject
모든 클래스 타입의 인스턴스를 나타낼 수 있습니다.
클래스, 클래스 타입 또는 클래스 전용 프로토콜의 인스턴스에 대한 구체적인 타입으로 사용될 수 있음
var anyArray: [AnyObject] = [1,true,"3",1.0] // **Error!
var** anyArray **: [AnyObject] = [1 as AnyObject,
"hi" as AnyObject,
true as AnyObject,
1.0 as AnyObject]**
프로토콜이기 때문
배열 안의 값들이 클래스 타입이 아니다.
클래스 타입을 넣으면 에러 해결
class AType {}
class BType {}
var anyObjectArray: [AnyObject] = [AType(), BType()]