文初:
如果你對(duì)swift的些許了解只局限在
- swift中的類型使用struct取代class
- 多了Optional可選類型
這些最基礎(chǔ)的認(rèn)知,而對(duì)其底層設(shè)計(jì)的原因和原理了解甚少隘击,那這篇文章會(huì)給你新的視角脊串,讓你更好的理解和使用。
為了讓你能更宏觀的理解swift在類型設(shè)計(jì)方面的初衷躏尉,本文會(huì)也會(huì)從更加宏觀的角度著手蚯根。
其實(shí)swift的類型,從大的層面有兩種類型:
- Named Types
- Compound Types
Named Types
其具體包含如下:
* class
* struct
{
* String
* Int
* Float
* Bool
* ...
}
* enum
* protocol
swift類型的新特點(diǎn)(區(qū)別與OC):
* 沒有特定的根類型(No dedicated type root )
* 類型通過遵守協(xié)議的方式胀糜,而非繼承來實(shí)現(xiàn)擴(kuò)展( Type conforms to protocols instead of heavy inheritance )
* 低耦合(Super loose coupling )
* 概念分離(Nice separation of concerns )
* 結(jié)構(gòu)清晰(Clean architecture )
對(duì)比OC颅拦,所有的對(duì)象類型都繼承自NSObject,其本質(zhì)是Class類型教藻。
為了保證繼承的完整性距帅,又多出了meta class等概念。Class中通過繼承來實(shí)現(xiàn)的方法的擴(kuò)展括堤。
比如:NSArray及NSMutableArray類型
NSMutableArray是NSArray的子類碌秸。子類NSMutableArray特有的addObject:
等方法是通過繼承NSArray,在子類中單獨(dú)實(shí)現(xiàn)。
這種設(shè)計(jì)本身無可挑剔悄窃,但OC過于依賴?yán)^承來實(shí)現(xiàn)方法的擴(kuò)展讥电,導(dǎo)致整個(gè)體系特別臃腫,過于耦合轧抗。
@interface AClass: NSObject
- (void)aMethod;
- (void)bMethod;
- (void)oneMethod;
- (void)twoMethod;
@end
//BClass想擁有aMethod和bMethod方法恩敌,不需要oneMethod和twoMethod,在OC一般都繼承實(shí)現(xiàn)
@interface BClass: AClass
@end
而swift不過度依賴Class,其擴(kuò)展了Struct横媚,Enum, Protocol纠炮,并給其更高的權(quán)限(可更加靈活的添加屬性,定義方法等)灯蝴,讓其變成一等公民恢口。
swift底層是結(jié)構(gòu)體,結(jié)構(gòu)體不是對(duì)象類型穷躁,自然沒辦法通過繼承來實(shí)現(xiàn)方法擴(kuò)展弧蝇。那其方法實(shí)現(xiàn)是通過何種方式完成的呢?
答案是協(xié)議折砸,不同的協(xié)議包裹了不同的方法看疗,通過遵守不同的協(xié)議來擴(kuò)展特定的方法。
對(duì)于上述需求睦授,在swift中一般通過如下方法實(shí)現(xiàn)
// swift通過protocol來擴(kuò)充方法两芳,通過遵守不同的協(xié)議來獲取不同方法,更偏重組合去枷,而非繼承
protocol FirstProtocol {
func aMethod()
func bMethod()
}
protocol SecondProtocol {
func oneMethod()
func twoMethod()
}
class AClass: FirstProtocol,SecondProtocol {
//實(shí)現(xiàn)兩個(gè)方法
}
class BClass: FirstProtocol {
//實(shí)現(xiàn)兩個(gè)方法
}
比如Array類型怖辆,其通過繼承不同的協(xié)議來獲取不同的方法是复。
* 類比理解:
繼承更講究出身,尤其OC是單繼承竖螃,只能有一個(gè)父類淑廊,你想獲取父類制定的方法,必須繼承父類特咆。
而面向協(xié)議則更加靈活季惩,不同的protocol封裝了不同的技能包,某個(gè)類型想要獲取對(duì)應(yīng)方法腻格,只需遵守該protocol就ok了画拾。而且一個(gè)類型可以遵守多個(gè)協(xié)議,更加靈活菜职。而且Enum,Struct都可以通過遵守協(xié)議來獲取方法青抛。
Compound Types (復(fù)合類型)
- Tuples(元組)
- Function(函數(shù))
Tuples Types
Tuples(元組)是swift的新類型。其成員不同酬核,Tuple類型就不同蜜另。
var aTuple = (x: 10, y: 20)
// 此時(shí)就會(huì)報(bào)錯(cuò),x的類型只能是Int
// aTuple = (x: "name", y: 50)
當(dāng)Tuples作為函數(shù)的返回值類型嫡意,就可以返回多個(gè)值
let nameDictionary: [Int: String] = [001: "Jimmy"]
let ageDictionary: [Int: Int] = [001: 20]
func getInformationFrom (ID: Int) -> (name: String, age: Int) {
let name = nameDictionary[ID] ?? "找不到對(duì)應(yīng)的名字"
let age = ageDictionary[ID] ?? 0
return (name, age)
}
Function Types
swift將Function(函數(shù))提升為一等類型
let nameDictionary: [Int: String] = [001: "Jimmy"]
let ageDictionary: [Int: Int] = [001: 20]
func getInformationFrom (ID: Int) -> (name: String, age: Int) {
let name = nameDictionary[ID] ?? "找不到對(duì)應(yīng)的名字"
let age = ageDictionary[ID] ?? 0
return (name, age)
}
// 用aPerson接受這個(gè)函數(shù)举瑰,類型為一個(gè)元組類型,值為(name: "Jimmy", age: 20)
let aPerson = getInformationFrom(ID: 001)
Blocks VS Closures(閉包)
swift中的closure類似于OC的block,但還是有些區(qū)別鹅很。
關(guān)于兩者語法的不同,可以參考
Block Syntax and Closure Syntax
Function VS Closures
A closure is actually a higher usage of a function type. Above, we just have two types of types; the closure is defined quite similarly to the function itself. This is because it takes the parameters and the area and the return type just as the type signage. This makes functions and closures siblings.
Optional可選類型
swift中的Optional底層就是一個(gè)帶泛型的枚舉類型罪帖。
其源碼抽象出來如下:
public enum Optional<Wrapped> : ExpressibleByNilLiteral {
case none
case some(Wrapped)
public init(_ some: Wrapped)
public init(nilLiteral: ())
}
// 此時(shí)定義中的Wrapped就是String類型
var myName : String? = "Walker"
print(myName)
print(myName!)
if let name = myName {
print(name)
}