/**

1.定义一个类层次作为例子

2.检查类型

向下转型(Downcasting)

和AnyObject的类型检查

类型检查是一种检查类实例的方式,并且或者也是让实例作为它的父类或者子类的一种方式

swift中使用is和as操作符实现,这连个操作符提供了一种简单达意的方式去检查值的类型或者转换它的类型

也可以用来检查一类是够实现了,某个协议

*/
 
 
class
var name: String;
init(name: String) {
self.name
     }
 }
 
class Movie: MediaItem
var director:  String;
init(name: String, director: String) {
self.director
super.init(name: name);
     }
 }
 
class Song: MediaItem
var artist: String;
init(name: String, artist: String) {
self.artist
super.init(name: name);
     }
 }
let
Movie(name: "万万没想到", director: "叫兽易小星"),
Movie(name: "屌丝男士", director: "大鹏"),
Song(name: "一次就好", artist: "杨宗纬"),
Song(name: "如果没有如果", artist: "林俊杰")
 ];
//检查类型
 /**
 is 来检查一个实例是否属于特定子类型。若实例属于那个子类型,类型检查操作符返回true,若不属于返回false
 */
var movieCount = 0;
var songCount = 0;
 //var mediaCount = 0;
for item in library
if item is Movie
movieCount++;
else if item is Song
songCount++;
     }
 //    if item is MediaItem {
 //        mediaCount++;
 //    }
 }
print("movieCount is \(movieCount)\nsongCount is \(songCount)");
 
 
//向下转型
 /**
as)

as?返回一个你试图下转成功的类型的可选值。强制形式as把试图向下转型和强制解包结果作为一个混合动作

详见资料

尝试将item转为Movie类型,若成功,设置一个新的临时常量movie来存储返回的可选Movie

转换没有真的改变实例或它的值,潜在的根本的实例保持不变,只是简单地把它作为它被转换成的类来使用

*/
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)");
     }
 }
 
 
和AnyObject的类型检查
let someObject: [AnyObject] = [
Movie(name: "港囧", director: "徐峥"),
Movie(name: "老炮", director: "管虎"),
Movie(name: "煎饼侠", director: "大鹏")
 ];
for object in someObject
let movie = object  as! Movie;
    print("Movie: '\(movie.name)', dir. \(movie.director)");
     
 //    if let movie = object as? Movie{
 //        print("Movie: '\(movie.name)', dir. \(movie.director)");
 //    }
 }
//为了变为一个更短的形式,下转someObjects数组为Movie[]类型来代替下转每一项方式
for movie in someObject as! [Movie] {
    print("Movie: '\(movie.name)', dir. \(movie.director)");
 }
 
类型
var things = [Any]();
.append(0);
.append(0.0);
.append(42);
things.append(3.14159);
things.append("hello");
things.append((3.0, 5.0));
things.append(Movie(name:"sfs", director: "gerg"));
for thing in things
switch
case 0  as Int:
        print("zero as an Int");
case 0  as Double:
        print("zero as an 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)");
    default:
        print("something else");
     }
 }
 /**
switch语句的case中使用强制形式的类型检查操作符(as,而不是as?)来检查和转换到一个明确的类型。在switch case语句的内容中这种检查总是安全的
 */