本文是學(xué)習(xí)《The Swift Programming Language》整理的相關(guān)隨筆富蓄,基本的語法不作介紹凤巨,主要介紹Swift中的一些特性或者與OC差異點婚瓜。
系列文章:
- Swift4 基礎(chǔ)部分:The Basics
- Swift4 基礎(chǔ)部分:Basic Operators
- Swift4 基礎(chǔ)部分:Strings and Characters
- Swift4 基礎(chǔ)部分:Collection Types
- Swift4 基礎(chǔ)部分:Control Flow
- Swift4 基礎(chǔ)部分:Functions
- Swift4 基礎(chǔ)部分:Closures
- Swift4 基礎(chǔ)部分: Enumerations
- Swift4 基礎(chǔ)部分: Classes and Structures
- Swift4 基礎(chǔ)部分: Properties
- Swift4 基礎(chǔ)部分: Methods
- Swift4 基礎(chǔ)部分: Subscripts
- Swift4 基礎(chǔ)部分: Inheritance
- Swift4 基礎(chǔ)部分: Initialization
- Swift4 基礎(chǔ)部分: Deinitialization
- Swift4 基礎(chǔ)部分: Automatic Reference Counting(自動引用計數(shù))
- Swift4 基礎(chǔ)部分: Optional Chaining(可選鏈)
- Swift4 基礎(chǔ)部分: Error Handling(錯誤處理)
Type casting is a way to check the type of an instance, or to treat
that instance as a different superclass or subclass from somewhere
else in its own class hierarchy.
- 類型轉(zhuǎn)換可以用來檢測實例的類型锡移,或者將實例看成父類或子類的實例。
為類型轉(zhuǎn)化定義一個類的層級(Defining a Class Hierarchy for Type Casting)
例子:
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")
];
檢查類型(Checking Type)
Use the type check operator (is) to check whether an instance is of a
certain subclass type.
- 利用檢查操作符
is
去檢查實例是否是一個特定的子類類型砚婆。
例子:
var movieCount = 0;
var songCount = 0;
for item in library {
if item is Movie {
movieCount += 1;
} else if item is Song {
songCount += 1;
}
}
print("Media library contains \(movieCount) movies and \(songCount) songs");
執(zhí)行結(jié)果:
Media library contains 2 movies and 3 songs
向下轉(zhuǎn)換(Downcasting)
A constant or variable of a certain class type may actually refer to
an instance of a subclass behind the scenes. Where you believe this is
the case, you can try to downcast to the subclass type with a type
cast operator (as? or as!).
- 當(dāng)一個常量或變量實際上是一個子類的實例時,可以使用
as? or as
向下轉(zhuǎn)化突勇。
例子:
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("Movie: \(song.name), by \(song.artist)");
}
}
執(zhí)行結(jié)果:
Media library contains 2 movies and 3 songs
Movie: Casablanca, dir. Michael Curtiz
Movie: Blue Suede Shoes, by Elvis Presley
Movie: Citizen Kane, dir. Orson Welles
Movie: The One And Only, by Chesney Hawkes
Movie: Never Gonna Give You Up, by Rick Astley
Any
與AnyObject
的類型轉(zhuǎn)換(Type Casting for Any and AnyObject)
Swift provides two special types for working with nonspecific types:
Any can represent an instance of any type at all, including function
types.
AnyObject can represent an instance of any class type.
Any
可以表示任何類型的實例包括函數(shù)類型装盯。Anyobject
可以表示任何類類型的實例。
例子:
var things = [Any]();
things.append(0);
things.append(0.1);
things.append((3.0,5.0));
things.append("hello");
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 positive double value of \(someDouble)");
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)")
default:
print("something else");
}
}
執(zhí)行結(jié)果:
zero as an Int
a positive double value of 0.1
an (x, y) point at 3.0, 5.0
a string value of "hello"
這里簡單擴展一下NSObject
,AnyObject
,Any
的區(qū)別与境。
例子
class Example{}
class ObjectExample:NSObject{}
var example:Example = Example();
var objectExample:ObjectExample = ObjectExample();
print(example is NSObject);
print(example is AnyObject);
print(objectExample is NSObject);
print(objectExample is AnyObject);
執(zhí)行結(jié)果
false
true
true
true
- 1.每一個
NSObject
對象都是AnyObject
,單并非每一個AnyObject
都是NSObject
验夯。- 2.
NSObject
Swift中需要顯示繼承。