当明白 @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" >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 Scottview plain copy to clipboard print ? formovIEinsomeObjectsas[MovIE]{println("MovIE:'\(movIE.name)',dir.\(movIE.director)")}//MovIE:'2001:ASpaceOdyssey',dir.StanleyKubrick//MovIE:'Moon',dir.DuncanJones//MovIE:'AlIEn',dir.RIDleyScott
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") }}
//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
总结以上是内存溢出为你收集整理的[Swift] Any VS AnyObject全部内容,希望文章能够帮你解决[Swift] Any VS AnyObject所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)