<aside>
🌀 如下代码,library
中的元素分别为 Movie
和 Song
类型,但是当你迭代访问这些元素时,会被推断为 MediaItem
类型,即它们的父类。当你需要用这些元素自身的类型来访问它们时,就需要进行类型转换。
</aside>
class MediaItem {
var name: String
init(name: String) {
self.name = name
}
}
class Movie: MediaItem {
var director: String
init(name: String, director: String) {
self.director = director
super.init(name: name)
}
}
class Song: MediaItem {
var artist: String
init(name: String, artist: String) {
self.artist = artist
super.init(name: name)
}
}
let library = [
Movie(name: "Casablanca", director: "Michael Curtiz"),
Song(name: "Blue Suede Shoes", artist: "Elvis Presley"),
Movie(name: "Citizen Kane", director: "Orson Welles"),
Song(name: "The One And Only", artist: "Chesney Hawkes"),
Song(name: "Never Gonna Give You Up", artist: "Rick Astley")
]
// 数组 library 的类型被推断为 [MediaItem]
<aside>
🌀 使用 is
判断一个实例对象的类型是否是某个子类。
</aside>
var movieCount = 0
var songCount = 0
for item in library {
if item is Movie {
movieCount += 1
} else if item is Song {
songCount += 1
}
}
print("Media library contains \\(movieCount) movies and \\(songCount) songs")
// 打印“Media library contains 2 movies and 3 songs”
<aside>
🌀 as!
与 as?
,一个会强制解包,另一个会返回一个可选值。
</aside>
for item in library {
if let movie = item as? Movie {
print("Movie: \\(movie.name), dir. \\(movie.director)")
} else if let song = item as? Song {
print("Song: \\(song.name), by \\(song.artist)")
}
}
// Movie: Casablanca, dir. Michael Curtiz
// Song: Blue Suede Shoes, by Elvis Presley
// Movie: Citizen Kane, dir. Orson Welles
// Song: The One And Only, by Chesney Hawkes
// Song: Never Gonna Give You Up, by Rick Astley
Any
和 AnyObject
的类型转换<aside>
🌀 Any
可以表示任何类型,包括函数类型。
AnyObject
可以表示任何类类型的实例。
</aside>
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:
print("zero as an Int")
case 0 as Double:
print("zero as a Double")
case let someInt as Int:
print("an integer value of \\(someInt)")
case let someDouble as Double where someDouble > 0:
print("a positive double value of \\(someDouble)")
case is Double:
print("some other double value that I don't want to print")
case let someString as String:
print("a string value of \\"\\(someString)\\"")
case let (x, y) as (Double, Double):
print("an (x, y) point at \\(x), \\(y)")
case let movie as Movie:
print("a movie called \\(movie.name), dir. \\(movie.director)")
case let stringConverter as (String) -> String:
print(stringConverter("Michael"))
default:
print("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
<aside>
🌀 Any
类型可以表示所有类型的值,包括可选类型。Swift
在你用 Any
类型来表示一个可选值时,会产生一个警告。如果确实想要用 Any
类型来承载可选值,可以用 as
操作符将其显示转换为 Any
:
</aside>
let optionalNumber: Int? = 3
things.append(optionalNumber) // 警告
things.append(optionalNumber as Any) // 没有警告