swift2 类型转换
来源:互联网 发布:excel表如何汇总数据 编辑:程序博客网 时间:2024/06/17 17:53
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 = 0var songCount = 0for item in library { if item is Movie { ++movieCount } else if item is Song { ++songCount }}print("Media library contains \(movieCount) movies and \(songCount) songs")// prints "Media library contains 2 movies and 3 songs"
向下转型
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类型转换
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 print("Movie: '\(movie.name)', dir. \(movie.director)")}// Movie: '2001: A Space Odyssey', dir. Stanley Kubrick// Movie: 'Moon', dir. Duncan Jones// Movie: 'Alien', dir. Ridley Scott
for movie in someObjects as! [Movie] { print("Movie: '\(movie.name)', dir. \(movie.director)")}// Movie: '2001: A Space Odyssey', dir. Stanley Kubrick// Movie: 'Moon', dir. Duncan Jones// Movie: 'Alien', dir. Ridley Scott
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") }}// 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
0 0
- swift2 类型转换
- Swift2.1 枚举转换类型
- swift2.0学习之类型转换
- swift2 集合类型
- swift2 枚举类型
- 《swift2.0 官方教程中文版》 第2章-19类型转换
- swift2
- swift2.0学习之类型方法(类方法)
- 《swift2.0 官方教程中文版》 第3章-02类型
- Swift2.2 学习笔记(十一) ___集合类型
- 自定义数据类型 --- 枚举类型全解(swift2.3)
- 自定义数据类型 --- 结构体类型全解(swift2.3)
- 类型转换
- 类型转换
- 类型转换
- 类型转换
- 类型转换
- 类型转换
- Android--sharepreference总结
- 求数列的和
- 中国象棋游戏Chess(1) - 棋盘绘制以及棋子的绘制
- 00-自测1. 打印沙漏(20)
- ./configure报错mv: cannot move './confsjjYiM/out' to 'cmake/Makefile': Permission denied
- swift2 类型转换
- 10 个你必须掌握的超酷 VI 命令技巧
- 机房收费系统之思路
- POJ 1185:炮兵阵地
- 00-自测2. 素数对猜想 (20)
- Hadoop2.5.2+ubuntu14.04+eclispe+hadoop2x-eclipse-plugin-master搭建开发环境
- final finally finalize
- 点击UITableView的cell展开收缩
- 人教版九年级数学的“词汇”中英文对照表翻译bug