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[]
- 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"
- for item in library {
- 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)")
- }
- }
- // 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
- 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
- println("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] {
- println("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"))
- 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。
swift:类型转换(is用作判断检测、as用作类型向下转换)的更多相关文章
- Swift - 类型转换(as as! as?)
swift 类型转换 一,as 1,as使用场合 (1)从派生类转换为基类,向上转型(upcasts) class Animal {} class Cat: Animal {} let cat = C ...
- Swift类型转换
关于「类型转换」(Type Casting),<The Swift Programming Language>描述如下: Type casting is a way to check th ...
- 判断是否联网_检测网络的类型为3G、2G、wap、wifi
判断是否联网_检测网络的类型为3G.2G.wap.wifi 判断是否联网: /*** * judge Internet is available * * @author wei-spring * @ ...
- Swift类型转换 和 类型别名的定义(typealias)
(一)类型转换 类型转化在 Swift 中是比较严格的,不同类型之间可以认为是不能相互转化的,只能重新产生一个对象和值,并拷贝一份. 1.0 整型数值之间的转换. // 不同类型是不能直接相加的,这时 ...
- php判断检测一个数组里有没有重复的值
php判断检测一个数组里有没有重复的值 php里有一个处理数组重复值得函数array_unique,我们的思路就是用这个函数来实现的. if (count($array) != count(array ...
- swift类型转换之Could not cast value of type xxx to xxx错误的一种特殊情况记录
之前swift项目打包成Framework静态库,提供给OC项目套入使用时,有时会抱这样一个错误: 这个错误发生的概率比较随机,有时会,有时不会,而且这句话在swift中的使用,是做model类型强制 ...
- jQuery 判断是否为数字的方法 及 转换数字函数
<script language="javascript"> var t=$("#id").val();//这个就是我们要判断的值了 if(!isN ...
- Swift类型检查与转换
继承会发生在子类和父类中,如图所示,是一系列类的继承关系类图,Person是类层次结构中的根类,Student是Person的直接子类,Worker是Person的直接子类.这个继承关系类图的具体实现 ...
- Swift—类型检查与转换-备
继承会发生在子类和父类之间,是一系列类的继承关系. 例如:Person是类层次结构中的根类,Student是Person的直接子类,Worker是Person的直接子类. 这个继承关系类的具体实现代码 ...
随机推荐
- 批量修改文件后缀(Python)
近期下载了很多各种教程, 但是不幸的是后缀名都是 ".mp4", 而本人喜欢 ".rmvb" 后缀,由于有轻微洁癖, 受不了后面的 ".mp4&quo ...
- 点击图标 标记为星标记事mac中修改默认的apache网站根目录位置
在Mac OS X中可以很方便的通过开启“Web共享”启用Apache服务:设置方法如下: 打开“系统设置偏好(System Preferences)” -> “共享(Sharing)” -&g ...
- ASP.NET MVC +EasyUI 权限设计(三)基础模块
请注明转载地址:http://www.cnblogs.com/arhat 在上一章中呢,我们基本上搭建好了环境,那么本章我们就从基础模块开始写起.由于用户,角色,动作三个当中,都是依赖与动作的,所以本 ...
- android开发 PopupWindow 设置充满屏幕
View qrcode_view = this.getLayoutInflater().inflate(R.layout.taskdetail_qrcode,null); final PopupWin ...
- 《Dive into Python》Chapter 4 笔记
自省:Python中万物皆对象,自省是指代码可以查看内存中以对象形式存在的其它模块和函数,获取它们的信息,并对它们进行操作.用这种方法,可以定义没有名称的函数,不按函数声明的参数顺序调用函数,甚至引用 ...
- UML类图关系-转
1.关联 双向关联: C1-C2:指双方都知道对方的存在,都可以调用对方的公共属性和方法. 在GOF的设计模式书上是这样描述的:虽然在分析阶段这种关系是适用的,但我们觉得它对于描述设计模式内的类关系来 ...
- dbutils 执行sql返回的数据类型
//ArrayHandler: 把结果集中的第一行数据转成对象数组 //ArrayListHandler:把结果集中的每一行数据都转成一个对象数组,再存放到List中 //BeanHandler:将结 ...
- Xcode Provisioning 路径
~/Library/MobileDevice/Provisioning Profiles
- BitNami一键安装Redmine(转)
1. 简介 对于一个新手,如果严格按照官方文档来安装redmine,我想会“疯”掉的.有没有一种简便的方法.有滴,那就是BitNami. BitNami提供redmine的一键安装程序,简单.易用.方 ...
- hdu 4901
一个简单的dp,比赛的时候太坚信自己的小聪明没用二维数组一直WA到死: #include<cstdio> #include<cstring> #define maxn 1009 ...