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的直接子类. 这个继承关系类的具体实现代码 ...
随机推荐
- 使用wget备份禅道
禅道7.1,管理了公司所有项目.需要每月备份. 主机安装在一台windows上.为了方便,写个脚本自动调用禅道的备份功能,并把服务器上的备份文件下载到本地. @echo off setlocal re ...
- How to using x++ creating Vendors [AX2012]
.//Create party for the vendor public void createParty(VendorRequestCreate _vendorRequestCreate) { ; ...
- TAG的用法和用途[转]
用一个例子来说明:一个combobox控件...一个textBox控件...一个datagridview控件!datagridview控件是连接数据库的...combobox和textBox是联合查询 ...
- OGG配置
准备安装和运行用户(操作系统用户) 建议使用oracle用户 也可以使用新建用户:但是需要做配置 必须缴入到oinstall 组 必须使用和oracle相同的profile 操作系统必须为该用户开放一 ...
- 无法解析的外部符号 _WinMain@16 fatal error LNK1120: 1 个无法解析的外部命令
一,问题描述MSVCRTD.lib(crtexew.obj) : error LNK2019: 无法解析的外部符号 _WinMain@16,该符号在函数 ___tmainCRTStartup 中被引用 ...
- Unity Camera属性
Camera属性 1.Clear Flags 清除标记:决定屏幕的那部分将被清除.当使用多个相机来描绘不同的游戏景象时,利用它是非常方便的. 2.Background 背景:在镜头中的所有元素描绘完成 ...
- 微软职位内部推荐-SDE2 (Windows driver)
微软近期Open的职位: SDE2 (Windows driver) Job title: Software Development Engineer 2 Location: Shanghai, Ch ...
- 如何在64位的Windows中安裝PLSQLDEVELOPER 8
先到 Oracle 官網下載Oracle Database 11g Release 2 Client (11.2.0.1.0) for Microsoft Windows (x64) ,接者依照以下步 ...
- android动态增加控件时控制样式的方法
在学习android的动画时,发现所谓的tween动画只是改变绘制效果并不改变原控件的位置时是颇为失望的,虽然3.0之后已经有了property animation,但是由于要兼容老版本的androi ...
- android 解决启动页面加载图片空白以及去掉标题栏
有时候启动页面根据白天晚上来启动时实现加载不同的图片效果,但是加载时会出现短暂的空白,解决方法如下: android:theme="@android:style/Theme.Transluc ...