来源  Official documents

  1. 定义一个类层次作为例子
  2. 类型检查
  3. 向下转型(简称下转)
  4. Any和AnyObject的转换 

 

类型转换在Swift中使用 isas 操作符实现。这两个操作符提供了一种简单达意的方式去检查值的类型或者转换它的类型。

类型检查操作符(is):检查一个实例是否属于特定子类型。若实例属于那个子类型,返回 true ,若不属于返回 false

类型转换操作符(as):有两种形式:

  • 可选形式(as?):如果转换成功返回可选值,失败返回nil
  • 强制形式:转换成功返回转换的类型,失败会触发一个runtime error。

 

定义一个类层次作为例子

class MediaItem { 
    var name: String 
    init(name: String) { 
        self.name = name 
    } 
} 

// 下面这两个类都是继承MediaItem
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[]类型

 

类型检查

用类型检查操作符(is)来检查一个实例是否属于特定子类型。若实例属于那个子类型,返回 true ,若不属于返回 false

var movieCount = 0 
var songCount = 0 
  
for item in library { 
    if item is Movie { 
        ++movieCount 
    } else if item is Song { 
        ++songCount 
    } 
} 

println("Media library contains \(movieCount) movies and \(songCount) songs") 

 

向下转型(简称下转)

for item in library { 
    if let movie = item as? Movie { 
        println("Movie: '\(movie.name)', dir. \(movie.director)") 
    } else if let song = item as? Song { 
        println("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的转换 

Swift为不确定类型提供了两种特殊类型别名:

  • AnyObject可以代表任何class类型的实例。
  • Any可以表示任何类型,除了函数类型。

 

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

// 还可以直接转为Movie[]数组类型
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 = A[ny]() 
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")) 
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)") 
    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。 

 

2015-03-24

20:11:38

 

posted on 2015-03-25 20:13  道无涯  阅读(110)  评论(0编辑  收藏  举报