1.AnyObject
定义:
public typealias AnyObject
说明:
The protocol to which all classes implicitly conform.
AnyObject can be used as the concrete type for an instance of any class, class type, or class-only protocol.
The flexible behavior of the AnyObject
protocol is similar to
Objective-C’s id
type.
AnyObject:用于表示任意类,元类的实例的具体类型.
对于”123”或123这些基础数据类型在Swift中是结构体类型,所以这里需要将其强转为AnyObject类型,此时它们的类型将是
OC的NSTaggedPointerString和__NSCFNumber等对象类型.不强转的话编译会出错.
eg:
1 | class FDEItemModel {} |
在使用AnyObject 对象时要特别注意如果你需要调用它的属性则最好先downcast为实际的类型,如果不downcast那么系统可能获取的是其他类的同名属性,得到的值将是nil。