当明白 @UIApplicationMain 的内涵段子后,又发现一个神奇的东西
func application(application: UIApplication,didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { // Override point for customization after application launch. return true }出现了 AnyObject,这是什么东西,[NSObject:AnyObject] 又是什么东西,感觉怪怪的
完全不适应呀,和之前的beta 版对比会发现社么呢???
原来是用[NSObject:AnyObject] 来替换 NSDictionary 的,那又是为什么呢,中括号又是干什么的?,“?”又是干嘛的
瞬间疑问更多了,首先是看到官方的解释
哦,原来 AnyObject 是无类型的实例呀,准确来说 Any 是无类型的任何东西的实例,可以使基本数据类型、方法、闭包等
是不是想到了万能的 Id 指针、void 这些东西,而且官方还让在知道类型的时候,不用他们俩,必要时才推荐用。。。
哦,或许会明白 Any、AnyObject 这俩的概念了 [NSObject:AnyObject] 是啥东西,和 NSDictionary 有啥关系呢?
第一反应就是,前者可能是 后者的一种形式,查阅资料才明白,原来是。。。 XXX
字典的声明有这么多:
A:varcreditDictionary=Dictionary<String,Int>()
B:varshorthandDictionary=[String:Int]()
C: var emptyDictionary =[:] (空字典也可以这样 ~~ OMG ~~)
这样我们就可以简单的理解 [] 就是取 实例的对应的类,然后我们也会见到类似数组的声明
varintArray=Array<Int>()
B:varshorthandArray=[Int]()
C:shorthandArray=[] (这样够空的了。。)
更多关于Swift 中的 NSArray 、NSDictionary 详见喵神:
http://onevcat.com/2014/06/walk-in-swift/
那么对问题有了了解后,? 是干嘛的呢,拆包呀,还有! 呢
详情解释见代码手工艺人解释:
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
- formovieinsomeObjectsas[Movie]{println("Movie:'\(movie.name)',dir.\(movie.director)")}//Movie:'2001:ASpaceOdyssey',dir.StanleyKubrick//Movie:'Moon',dir.DuncanJones//Movie:'Alien',dir.RidleyScott
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") }}
//zeroasanInt//zeroasaDouble//anintegervalueof42//apositivedoublevalueof3.14159//astringvalueof"hello"//an(x,y)pointat3.0,5.0//amoviecalled'Ghostbusters',dir.IvanReitman//Hello,Michael
其他相关
喵神贴
http://swifter.tips/any-anyobject/
官方资料
https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/TypeCasting.html
原文链接:https://www.f2er.com/swift/326094.html