swift学习笔记之-类型转换

//类型转换

import UIKit

/*类型转换(Type Casting)

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

2.类型转换使用 is 和 as 操作符实现。用这两个操作符来检查值的类型或者转换它的类型,也可以用它来检查一个类型是否实现了某个协议

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

向下转型(Downcasting):

1.某类型的一个常量或变量可能在幕后实际上属于一个子类。当确定是这种情况时,你可以尝试向下转到它的子类型,用类型转换操作符(as? 或 as!)

2.因为向下转型可能会失败,类型转型操作符带有两种不同形式。条件形式(conditional form)as? 返回一个你试图向下转成的类型的可选值(optional value)。强制形式 as! 把试图向下转型和强制解包(force-unwraps)转换结果结合为一个操作。

3.当你不确定向下转型可以成功时,用类型转换的条件形式(as?),转型成功返回一个可选值,失败则返回nil

4.只有你可以确定向下转型一定会成功时,才使用强制形式(as!),转型为一个不正确的类型时,则触发运行时错误

Any 和 AnyObject 叫做不确定类型,

1.AnyObject可以表示任何类class类型的实例,可以用is和as来检查和转换它为更明确的类型

2.Any可以表示任何类型,包括函数类型,可以用is和as来检查和转换它为更明确的类型

*/

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)

}

}

//类型检测器能够推断出 Movie 和 Song 有共同的父类 MediaItem,所以它推断出library 的类型为 [MediaItem]

//在幕后 library 里存储的媒体项依然是 Movie 和 Song 类型的

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

]

var movieCount = 0

var songCount = 0

//=========== is 类型检查 ============

for item in library {

if item is Movie {

++movieCount

} else if item is Song {

++songCount

}

}

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

// 打印 “Media library contains 2 movies and 3 songs”

//=============== 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)")

}

}

//===========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 movie in someObjects as! [Movie] {

print("Movie: '\(movie.name)', dir. \(movie.director)")

}

//===========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:

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

}

}

上一篇:获取subview


下一篇:C/C++ 宏中的 #、#@、##的作用