[Swift] Any VS AnyObject
当明白 @UIApplicationMain 的内涵段子后,又发现一个神奇的东西
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { // Override point for customization after application launch. return true }
出现了 AnyObject , 这是什么东西,[NSObject:AnyObject] 又是什么东西,感觉怪怪的
完全不适应呀,和之前的beta 版对比会发现社么呢???
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool { // Override point for customization after application launch. return true }
原来是用[NSObject:AnyObject] 来替换 NSDictionary 的,那又是为什么呢,中括号又是干什么的?,“?”又是干嘛的
瞬间疑问更多了,首先是看到官方的解释
哦,原来 AnyObject 是无类型的实例呀,准确来说 Any 是无类型的任何东西的实例,可以使基本数据类型、方法、闭包等
是不是想到了万能的 Id 指针、void 这些东西,而且官方还让在知道类型的时候,不用他们俩,必要时才推荐用。。。
哦,或许会明白 Any、AnyObject 这俩的概念了 [NSObject:AnyObject] 是啥东西,和 NSDictionary 有啥关系呢?
第一反应就是,前者可能是 后者的一种形式,查阅资料才明白,原来是。。。 XXX
字典的声明有这么多:
A: var creditDictionary = Dictionary<String,Int>()
B: var shorthandDictionary = [String:Int]()
C: var emptyDictionary = shorthandDictionary = [:] (空字典也可以这样 ~~ OMG ~~)
这样我们就可以简单的理解 [] 就是取 实例的对应的类,然后我们也会见到类似数组的声明
A: var intArray = Array<Int>()
B: var shorthandArray = [Int]()
C: shorthandArray = [] (这样够空的了。。)
更多关于Swift 中的 NSArray 、NSDictionary 详见喵神:
那么对问题有了了解后,? 是干嘛的呢,拆包呀,还有! 呢
详情解释见 代码手工艺人 解释:
http://www.cocoachina.com/swift/20140605/8687.html
最后贴个官方的相关代码吧
AnyObject:
let someObjects: [AnyObject] = [ Movie(name: "2001: A Space Odyssey", director: "Stanley Kubrick"), Movie(name: "Moon", director: "Duncan Jones"), Movie(name: "Alien", director: "Ridley Scott") ]
<pre name="code" class="html">for object in someObjects { let movie = object as Movie println("Movie: '\(movie.name)', dir. \(movie.director)") } // Movie: '2001: A Space Odyssey', dir. Stanley Kubrick // Movie: 'Moon', dir. Duncan Jones // Movie: 'Alien', dir. Ridley Scott
for movie in someObjects as [Movie] { println("Movie: '\(movie.name)', dir. \(movie.director)") } // Movie: '2001: A Space Odyssey', dir. Stanley Kubrick // Movie: 'Moon', dir. Duncan Jones // Movie: 'Alien', dir. Ridley Scott
Any:
var things = [Any]() things.append(0) things.append(0.0) things.append(42) things.append(3.14159) things.append("hello") things.append((3.0, 5.0)) things.append(Movie(name: "Ghostbusters", director: "Ivan Reitman")) things.append({ (name: String) -> String in "Hello, \(name)" })
for thing in things { switch thing { case 0 as Int: println("zero as an Int") case 0 as Double: println("zero as a Double") case let someInt as Int: println("an integer value of \(someInt)") case let someDouble as Double where someDouble > 0: println("a positive double value of \(someDouble)") case is Double: println("some other double value that I don't want to print") case let someString as String: println("a string value of \"\(someString)\"") case let (x, y) as (Double, Double): println("an (x, y) point at \(x), \(y)") case let movie as Movie: println("a movie called '\(movie.name)', dir. \(movie.director)") case let stringConverter as String -> String: println(stringConverter("Michael")) default: println("something else") } }
<pre name="code" class="html">for thing in things { switch thing { case 0 as Int: println("zero as an Int") case 0 as Double: println("zero as a Double") case let someInt as Int: println("an integer value of \(someInt)") case let someDouble as Double where someDouble > 0: println("a positive double value of \(someDouble)") case is Double: println("some other double value that I don't want to print") case let someString as String: println("a string value of \"\(someString)\"") case let (x, y) as (Double, Double): println("an (x, y) point at \(x), \(y)") case let movie as Movie: println("a movie called '\(movie.name)', dir. \(movie.director)") case let stringConverter as String -> String: println(stringConverter("Michael")) default: println("something else") } }
// zero as an Int // zero as a Double // an integer value of 42 // a positive double value of 3.14159 // a string value of "hello" // an (x, y) point at 3.0, 5.0 // a movie called 'Ghostbusters', dir. Ivan Reitman // Hello, Michael
其他相关
喵神贴
http://swifter.tips/any-anyobject/
官方资料
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。