Type Casting

作者: 宋奕Ekis | 来源:发表于2021-08-23 17:03 被阅读0次

Checking Type

We use is keyword to check if an instance is a specific class.

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")
]
// the type of "library" is inferred to be [MediaItem]

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")
// Prints "Media library contains 2 movies and 3 songs"

Downcasting

We can use as? and as! To downcast an instance to its subclass.

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

Type Casting for Any and AnyObject

We can use Any and Anyobject to be an alternative as a common type.

  • Any can represent an instance of any type at all, including function types.
  • AnyObject can represent an instance of any class type.
let optionalNumber: Int? = 3
things.append(optionalNumber)        // Warning
things.append(optionalNumber as Any) // No warning

Let’s think!

相关文章

  • The Swift Programming Language--

    Type Casting Type check operator: is Type cast operator: ...

  • Type casting

    float to double, 注意数值范围不能溢出。 将一个 int, short, long 赋给一个 ch...

  • Type Casting

    当数组中存有同一父类的不同子类时,该数组内容会被统一认定为是父类 is和as的区别:is用来判断,返回Bool;a...

  • Type Casting

    Checking Type We use is keyword to check if an instance i...

  • swift入门18 类型转换

    Type casting in Swift is implemented with the is and as o...

  • Swift Type Casting

    在Swift中,有两种方式实现类型转换:is 和 as 可以在类的层级中检查类实例的类型,或者在同一个类的层级中转...

  • Language Guide --- Type Casting

    swift中的类型转换使用is 和 as 操作符。 Checking Type 使用is操作符判断一个实例是否是特...

  • Type Casting(类型转换)

    //类型转换//“类型转换 可以判断实例的类型,也可以将实例看做是其父类或者子类的实例。”//“类型转换在 Swi...

  • Type Casting (类型转换)

    Type castingis a way to check the type of an instance, or...

  • 【Swift 3.1】19 - 类型转换 (Type Casti

    【Swift 3.1】19 - 类型转换 (Type Casting) 自从苹果2014年发布Swift,到现在已...

网友评论

    本文标题:Type Casting

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