码迷,mamicode.com
首页 > 编程语言 > 详细

swift2.0学习之类型转换

时间:2015-08-06 22:18:12      阅读:193      评论:0      收藏:0      [点我收藏+]

标签:swift   编程   

  (一)类型转换(type casting)

类型转换是检查类型实例的方式,也是把类型实例作为子类或者父类的方式

主要有三个关键词,is,as?,as!(as),is表示是某种类型(比如父类和子类同时出现,选择父类,就是表示层级比较大的那个类型),as表示的是向下转换(downcast)到子类本身,问号表示可选,叹号表示强拆包

举个例子:

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)
    }
}
<pre name="code" class="plain">        for item in library {
            
            if let movie = item as? Movie {
                print("name:\(movie.name), director:\(movie.director)")
            }else if let song = item as? Song {
                print("name:\(song.name), artist:\(song.artist)")
            }
        }

class Song: MediaItem { var artist: String init(name: String, artist: String) { self.artist = artist super.init(name: name) }}



        let library = [Movie(name: "张学友", director: "麦兆辉"),Song(name: "一路上有你", artist: "张学友"),Movie(name: "周星驰", director: "周星驰"),Song(name: "夕阳醉了", artist: "张学友"),Movie(name: "赌神", director: "王晶")]
        for item in library {
        
            if item is Movie {
                movieCount++
            }else if item is Song {
                songCount++
            }
            
        }
        
        print("Media library contains \(movieCount) movies and \(songCount) songs")

解释:

如果当前的MediaItem(item)是Movie的实例的话,就返回true,否则就返回false

而上面的item实际上是MediaItem类型,而不是真正的Movie类型,也就说你访问不到Movie的director的属性,那我们怎么才能让item实例真正是Movie本身的类型呢?这时我们就用到关键字as了,进行向下拆包

as有两种形式,一个是as?,表示返回的类型可选,一个是as!强制拆包,用这个的时候你要确保你解析的东西是正确有值的,可以解包成功的

        for item in library {
            
            if let movie = item as? Movie {
                print("name:\(movie.name), director:\(movie.director)")
            }else if let song = item as? Song {
                print("name:\(song.name), artist:\(song.artist)")
            }
        }

因为item是MediaItem的实例,向下可能是Movie的实例,所以用as?可选,再用一个可选绑定,如果成功,就可以访问director属性了

注意:转换实际上没有改变这个实例或者它的值,潜在的实例还是保持原来的值,它仅仅是被转换的类型的实例

(二)Any和AnyObject

AnyObject:代表任意类类型(class type)的实例

Any:可以代表任意类型的实例,除了函数类型

a.AnyObject

        let someObjects: [AnyObject] = [Movie(name: "大圣归来", director: "未知"), Movie(name: "速度与激情7", director: "温世仁"), Movie(name: "无间道", director: "刘伟强")]
        
        for item in someObjects {
            
            let movie = item as! Movie
            print("name: \(movie.name),dir.\(movie.director)")
        }

这里你已经确定item就是Movie的实例类型,直接强拆,用一个常量接收

还有一个办法,就是既然你已经确定someObjects是一个[Movie]类型的数组,那么就可以这么写:

        for movie in someObjects as! [Movie] {
            
            print("name: \(movie.name),dir.\(movie.director)")
        }

b.Any

既然Any除了函数类型都能代表,那么我们就定义一个可变数组,Any型的,装它几种类型

        var things = [Any]()
        things.append(0)
        things.append(0.0)
        things.append(42)
        things.append(3.14159)
        things.append("hell0")
        things.append((3.0, 5.0))
        things.append(Movie(name: "恐怖游轮", director: "未知"))
        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 double value of \(someDouble)")
            case is Double:
                print("some other double 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 is 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("xiaoyu"))
            default:
                print("something else")
                
            }
        }

版权声明:本文为博主原创文章,未经博主允许不得转载。

swift2.0学习之类型转换

标签:swift   编程   

原文地址:http://blog.csdn.net/rainshenji/article/details/47322411

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!