swift Any和AnyObject的类型转换

Any AnyObject是两种不确定类型

  • AnyObject标识任何类类型的实例
  • Any可以用来表示任何类型,包括类、函数、基本数据类型等
class Person{
    var name:String
    init (name:String){
        self.name = name
    }
}

class Teacher:Person{
    var school:String
    init (name:String, school:String){
        self.school = school
        super.init(name: name)
    }
}

class Stutent:Person{
    var age:Int
    init (age:Int, name:String){
        self.age = age
        super.init(name: name)
    }
}

let tea = Teacher(name:"老师", school:"北工大")
let stu = Stutent(age:23, name:"学生")
let array1:[AnyObject] = [tea, stu];
let array2:[Any] = [tea, stu];
let array3:[Any] = [tea, stu, "helloWorld"];
在实际工作中 AnyObject 经常用于字典和数组
看起来Any能代替AnyObject。

你可能感兴趣的:(swift Any和AnyObject的类型转换)