类型检查在 Swift 中使用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) } } 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]@H_403_24@library的储存的是Movie和Song的实例,若你迭代它,则取出的会是其父类MediaItem类型,而不是Movie和Song类型。为了让它们作为它们本来的类型工作,你需要检查它们的类型或者向下转换它们的类型到其它类型。 @H_403_24@通过操作符is来检查一个实例是否属于特定的子类型,返回true则属于
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") // prints "Media library contains 2 movies and 3 songs"@H_403_24@向下转型 @H_403_24@通过类型检查操作符as可向下转型.但是由于向下转型可能会失败 @H_403_24@故有两种形式①可选形式as? ②强制形式as!
当你确定转型一定会成功的时候使用as!
for item in library { //由于item是一个MediaItem类型的实例,它可能是一个Movie,故使用as? 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)") } }
Any和AnyObject类型检查
@H_403_24@Swift为不确定类型提供了两种特殊类型别名:①AnyObject可以代表任何class类型的实例。
②Any可以表示任何类型,除了方法类型(function types)。
@H_403_24@注意:只有当你明确的需要它的行为和功能时才使用Any和AnyObject。在你的代码里使用你期望的明确的类型总是更好的。 @H_403_24@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") ]@H_403_24@由于这个数组中只包含Movie实例,所以你可以直接使用as!来进行强制解包
for object in someObjects { let movie = object as! Movie }
或者直接将[AnyObject]强转成[Movie]
for movie in someObjects as! [Movie] { println("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中包含2个Int,2个Double,1个String,1个元组 (Double,Double),一个Movie对象
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
类型嵌套
一个类型中嵌套另一个类型,将需要嵌套的类型的定义写在被嵌套类型的区域{}内,而且可以根据需要定义多级嵌套
struct BlackjackCard { // 嵌套定义枚举型Suit enum Suit: Character { case Spades = "♠",Hearts = "♡",Diamonds = "♢",Clubs = "♣" } // 嵌套定义枚举型Rank enum Rank: Int { case Two = 2,Three,Four,Five,Six,Seven,Eight,Nine,Ten case Jack,Queen,King,Ace struct Values { let first: Int,second: Int? } var values: Values { switch self { case .Ace: return Values(first: 1,second: 11) case .Jack,.Queen,.King: return Values(first: 10,second: nil) default: return Values(first: self.toRaw(),second: nil) } } } // BlackjackCard 的属性和方法 let rank: Rank,suit: Suit var description: String { var output = "suit is \(suit.toRaw())," output += " value is \(rank.values.first)" if let second = rank.values.second { output += " or \(second)" } return output } }
枚举型的 Suit 用来描述扑克牌的四种花色,并分别用一个 Character 类型的值代表花色符号。
枚举型的Rank用来描述扑克牌从Ace~10,J,Q,K,13张牌,并分别用一个Int类型的值表示牌的面值。(这个Int类型的值不适用于Ace,K的牌)。
@H_403_24@举型Rank在自己内部定义了一个嵌套结构体Values。这个结构体包含两个变量,只有Ace有两个数值,其余牌都只有一个数值。结构体Values中定义的两个属性:first,为Int ; second,为 Int?,或 “optional Int”Rank定义了一个计算属性values,这个计算属性会根据牌的面值,用适当的数值去初始化Values实例,并赋值给values。对于J,Ace会使用特殊数值,对于数字面值的牌使用Int类型的值。
BlackjackCard结构体自身有两个属性—rank与suit,也同样定义了一个计算属性description,description属性用rank和suit的中内容来构建对这张扑克牌名字和数值的描述,并用可选类型second来检查是否存在第二个值,若存在,则在原有的描述中增加对第二数值的描述。
因为BlackjackCard是一个没有自定义构造函数的结构体,在Memberwise Initializers for Structure Types中知道结构体有默认的成员构造函数,所以你可以用默认的initializer去初始化新的常量theAceOfSpades:
@H_403_24@let theAceOfSpades = BlackjackCard(rank: .Ace,suit: .Spades) println("theAceOfSpades: \(theAceOfSpades.description)") // 打印出 "theAceOfSpades: suit is ♠,value is 1 or 11"
尽管Rank和Suit嵌套在BlackjackCard中,但仍可被引用,所以在初始化实例时能够通过枚举类型中的成员名称单独引用。在上面的例子中description属性能正确得输出对Ace牌有1和11两个值。
类型嵌套的引用
@H_403_24@在外部对嵌套类型的引用,以被嵌套类型的名字为前缀,加上所要引用的属性名:let heartsSymbol = BlackjackCard.Suit.Hearts.toRaw() // 红心的符号 为 "♡"原文链接:https://www.f2er.com/swift/326209.html