Any
代表任意类型(枚举、结构体、类,也包括函数类型或者Optional类型)
var stu: Any = 10
// 创建1个能存放任意类型的数组
stu = "Jack"
stu = Student()
// 创建1个能存放任意类型的数组
// var data = Array<Any>()
var data = [Any]()
data.append(1)
data.append(3.14)
data.append(Student())
data.append("Jack")
data.append({ 10 })
AnyObject
代表任意类类型(在协议后面写上: AnyObject代表只有类能遵守这个协议)
-
可以是instance 也可以是类
-
在协议后面写上: class也代表只有类能遵守这个协议
var t = TGTeacher()
// AnyObject 表代是对象实例类型
var t1: AnyObject = t
// AnyObject 表代是TGTeacher类的类型
var t2: AnyObject = TGTeacher.self
protocol Runnable: AnyObject{}
class Person: Runnable {}
struct Student:Runnable {}
//报错: error: non-class type 'Student' cannot conform to class protocol 'Runnable'
AnyClass
代表任意实例的类型: AnyObject.Type
var anyType: AnyObject.Type = Person.self
anyType = Student.self
public typealias AnyClass = AnyObject.Type
var anyType2: AnyClass = Person.self
anyType2 = Student.self
T.self
- T.self是一个元类型(metadata)的指针,metadata存放着类型相关信息
- 如果T是实例对象,返回的就是它本身; T是类,那么返回的是Metadata
class Person {}
class Student : Person {}
var perType: Person.Type = Person.self//perType Person前8个字节,存放这metadata地址值
var stuType: Student.Type = Student.self
perType = Student.self
T.Type
是一种类型,T.self属于T.Type类型
type(of:)
用来获取一个值的类型
var per = Person()
var perType = type(of: per) //取出person对象的前8个字节 即元类型 Person.self
print(Person.self == type(of: per)) // true
is
is用来判断是否为某种类型
protocol Runnable { func run() }
class Person {}
class Student : Person,Runnable {
func run() {
print("Student run" )
}
func study() {
print ("Student study")
}
}
var stu: Any = 10
print(stu is Int) // true
stu = "Jack"
print(stu is String) // true
stu = Student()
print(stu is Person) // true
print(stu is Student) // true
print(stu is Runnable) // true
as
as用来做强制类型转换
protocol Runnable { func run() }
class Person {}
class Student : Person,Runnable {
func run() {
print("Student run" )
}
func study() {
print ("Student study")
}
}
var stu :Any = 10
(stu as? Student)?.study() //没有调用 study
stu = Student( )
(stu as? Student)?.study() //Student study
(stu as! Student).study() // Student study
(stu as? Runnable)?.run() // Student run
var data = [Any]()
//类型一致的时候可以用as ,肯定可以转的时候
data. append(Int("123") as Any ) //数组里面可以添加可选项类型
var d=10 as Double
print(d) // 10.0
元类型的应用
- 元类型创建对象
class Animal { required init() {} }
class Cat : Animal {}
class Dog : Animal {}
class Pig : Animal {}
func create(_ clses: [Animal.Type]) -> [Animal] {
var arr = [Animal]()
for cls in clses {
arr.append(cls.init())
}
return arr
}
print(create([Cat.self, Dog.self, Pig.self]))
- 元类型作为参数
class Person {
var age: Int = 0
}
class Student : Person {
var no: Int = 0
}
print(class_getInstanceSize(Student.self)) // 32
print(class_getSuperclass(Student.self)!) // Person
print(class_getSuperclass(Person.self)!) // Swift._SwiftObject
//从结果可以看得出来,Swift还有个隐藏的基类:Swift._SwiftObject
//可以参考Swift源码:https://github.com/apple/swift/blob/master/stdlib/public/runtime/SwiftObject.h
Self
- Self代表当前类型
class Person {
var age = 1
static var count = 2
func run() {
print(self.age) // 1
print(Self.count) // 2
}
}
- Self一般用作返回值类型,限定返回值跟方法调用者必须是同一类型(也可以作为参数类型)
protocol Runnable {
func test() -> Self
}
class Person : Runnable {
required init() {}
func test() -> Self { type(of: self).init() }
}
class Student : Person{ }
var p = Person()
// Person
print(p.test())
var stu = Student()
// Student
print(stu.test())