美文网首页
18、【Swift】类型转换

18、【Swift】类型转换

作者: Sunday_David | 来源:发表于2020-12-28 22:17 被阅读0次
  • 使用场景:
    • 判断实例的类型
    • 转换实例的类型
  • 检查类型: is
  • 转换类型: as
  • 检查是否遵循某个协议

为类型转换定义类层次

  • 在类和子类的层次结构上,检查特定类实例的类型并且转换这个类实例的类型成为这个层次结构中的其他类型。
  • 基类 MediaItem。这个类为任何出现在数字媒体库的媒体项提供基础功能
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)
    }
}
  • Swift 的类型检测器能够推断出 MovieSong 有共同的父类 MediaItem,所以它推断出 [MediaItem] 类作为 library 的类型:
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]
  • library 里存储的媒体项依然是 MovieSong 类型的
  • 遍历取出的实例会是 MediaItem 类型的,而不是 MovieSong 类型

检查类型

  • 操作符is):检查一个实例是否属于特定子类型
  • 属于子类型,返回 true,否则返回 false
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”

向下转(子类)型

  • 类型转换操作符as?as!)向下转到它的子类型

  • 条件形式 as? 返回向下转型的可选值,转型失败,返回 nil

  • 强制形式 as! ,在 as? 继承上进行强制解包,失败触发运行时错误。

  • 事前你不知道每个 item 的真实类型,所以这里使用条件形式的类型转换(as?)去检查
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 的类型转换

  • 使用场景:为不确定类型提供了两种特殊的类型别名

    • 如新建一个存放数据的数组;
    • 网络获取的不确定类型的数据
  • Any 可以表示任何类型,包括函数类型

  • AnyObject 表示任何类(class)类型的实例

  • 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)" })
  • 可在 switch 表达式的 case 中,用 isas 找出 AnyAnyObject 类型的常量或变量的具体类型
    case is Double:
        print("some other double value that I don't want to print")
    case let someString as String:
  • 确定 any 类型数组元素的真实类型
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

注意

Any 类型可表示所有类型的值,包括可选类型。

Swift 会在你用 Any 类型来表示一个可选值的时候,给你一个警告。

如果你确实想使用 Any 类型来承载可选值,你可以使用 as 操作符显式转换Any,如下所示:

let optionalNumber: Int? = 3
things.append(optionalNumber)        // 警告
things.append(optionalNumber as Any) // 没有警告

相关文章

网友评论

      本文标题:18、【Swift】类型转换

      本文链接:https://www.haomeiwen.com/subject/hjhxoktx.html