Swift - AnyObject与Any的区别
//AnyObject 代表任何class类型的对象实例 let man = Man.init() let woman = Woman.init() let arr:[AnyObject] = [man,woman] for people in arr { if let m = people as? Man{ print("你是\(m)") }else if let w = people as? Woman{ print("你是\(w)") } }
//Any代表除函数以外任何类型实例 var anyArr:[Any] = [Any]() anyArr.append(1) anyArr.append("swift.com") anyArr.append(man) anyArr.append(woman) for any in anyArr { switch any { case let any as Int: print("Int类型\(any)") case let any as String: print("字符串类型\(any)") case let any as Man: print("男人\(any)") case let any as Woman: print("这是女人\(any)") default: print("未知类型") } }