1. 程式人生 > >[Swift] Any VS AnyObject

[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 = [:] (空字典也可以這樣 ~~ OMG ~~)

這樣我們就可以簡單的理解 [] 就是取 例項的對應的類,然後我們也會見到類似陣列的宣告

A: var intArray = Array<Int>()  

B: var shorthandArray = [Int]()

C: shorthandArray = [] (這樣夠空的了。。)

更多關於Swift 中的 NSArray 、NSDictionary 詳見喵神:

那麼對問題有了瞭解後,? 是幹嘛的呢,拆包呀,還有! 呢

最後貼個官方的相關程式碼吧

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")
    }
}
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

其他相關

喵神貼

官方資料