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的直接子类. 这个继承关系类的具体实现代码 ...
随机推荐
- 1095. Cars on Campus (30)
Zhejiang University has 6 campuses and a lot of gates. From each gate we can collect the in/out time ...
- orcale 修改字段属性
有些时候,因为没能预料到一些情况的变化,需要修改字段的类型.如果是varchar型,直接增加长度是可以的,但是如果需要修改成其他类型就不能这么做了. 思路:1.增加一个临时列,把需要修改的那个字段的数 ...
- python之函数嵌套
python很多特性与JavaScript是相似甚至相同的: 1. 无类型 2. 函数亦对象 .... 自然: python也允许函数嵌套, 这与JavaScript中函数闭包的作用一样....
- ubuntu 12.04版本出现界面终端打开broken pipe,但是tty1这些可以。
sudo apt-get remove xserver-xorg sudo apt-get install xserver-xorg
- WebAPi(selfhost)
ASP.NET WebAPi(selfhost)之文件同步或异步上传 前言 前面我们讲过利用AngularJs上传到WebAPi中进行处理,同时我们在MVC系列中讲过文件上传,本文结合MVC+We ...
- SQL Server DATEADD() 函数
SQL Server Date 函数 定义和用法 DATEADD() 函数在日期中添加或减去指定的时间间隔. 语法 DATEADD(datepart,number,date) date 参数是合法的日 ...
- [SC] OpenSCManager FAILED 1722
在服务器A(windows server 2008 r2)执行如下命令访问远端服务器B(windows server 2003)的服务运行状况: sc \\servername query " ...
- DB天气app冲刺二阶段第九天
今天是第九天了 不管怎么样也要收尾了赶紧,毕竟不可能做到尽善尽美了,时间不够了所以要把该砍掉的砍点,然后应对下周的大二同学的面试.尽量做好界面的美化工作这是最基本的了.毕竟我一直崇尚的就是UI设计了. ...
- linux-CentOS6.4下安装oracle11g详解
参考地址:http://dengqsintyt.iteye.com/blog/1991930
- highCharts 电流表、电压表
var highChartsSettingV = { chart: { margin: [5, 2, 5, 8], type: 'gauge', plotBorderWidth: 1, plotBac ...