下面列舉的都是一些比較偏僻的,特有的,很多人不知道的協(xié)議特性。
更多查看:https://segmentfault.com/a/1190000010065953
雖然我們不能在協(xié)議中直接提供屬性和方法的默認(rèn)實(shí)現(xiàn),但是我們可以通過協(xié)議擴(kuò)展來達(dá)到此目的。
甚至可以在擴(kuò)展中添加協(xié)議里沒有定義過的方法和屬性。
在協(xié)議中使用 static 關(guān)鍵字來聲明類型方法,如果實(shí)現(xiàn)該協(xié)議的類型是 class 類型,則在實(shí)現(xiàn)類中除了用 static 來修飾類型方法外,也可以使用 class關(guān)鍵字。區(qū)別是 static 修飾的屬性或方法不能被子類復(fù)寫,class 修飾的可以被子類復(fù)寫。
如果協(xié)議中定義的實(shí)例方法會(huì)改變遵循該協(xié)議的類型的實(shí)例,那么需要在該方法前加 mutating 關(guān)鍵字, 表示可以在該方法中修改它所屬的實(shí)例以及實(shí)例的任意屬性的值。
protocol Togglable {
mutating func toggle()
}
enum OnOffSwitch: Togglable {
case off, on
mutating func toggle() {
switch self {
case .off:
self = .on
case .on:
self = .off
}
}
}
var lightSwitch = OnOffSwitch.off
lightSwitch.toggle()
// lightSwitch 現(xiàn)在的值為 .On
- 協(xié)議中定義get、set屬性
protocol MyProtocol {
var prop: Int { get set }
var propertyWithImplementation: String { get }
func foo()
}
extension MyProtocol {
var propertyWithImplementation: String {
return "foo"
}
func foo() {
print(prop)
}
}
class MyClass: MyProtocol {
var prop: Int = 29
}
- 協(xié)議中還可以要求遵循協(xié)議的類型實(shí)現(xiàn)指定的構(gòu)造器:
protocol SomeProtocol {
init(someParameter: Int)
}
// 如果類為final,則不必加required標(biāo)識(shí)
class SomeClass: SomeProtocol {
required init(someParameter: Int) {
// initializer implementation goes here
}
Swift 可以采用 & 符號(hào)將多個(gè)協(xié)議進(jìn)行組合。
typealias Property = Named & Aged協(xié)議還可以與類進(jìn)行組合:
class City: Location, Named {
var name: String
init(name: String, latitude: Double, longitude: Double) {
self.name = name
// Location協(xié)議實(shí)例化
super.init(latitude: latitude, longitude: longitude)
}
}
- 判斷某個(gè)對(duì)象是否是某個(gè)類型用 is。在擴(kuò)展協(xié)議的時(shí)候,也可以指定一些限制條件,只有遵循協(xié)議的類型滿足這些限制條件時(shí),才能獲得協(xié)議擴(kuò)展提供的默認(rèn)實(shí)現(xiàn)。
extension Collection where Iterator.Element: TextRepresentable {
var textualDescription: String {
let itemsAsText = self.map { $0.textualDescription }
return "[" + itemsAsText.joined(separator: ", ") + "]"
}
}
let circulars = [Circular(radius: 1), Circular(radius: 2)]
print(circulars.textualDescription)
// [The circular's radius is 1, The circular's radius is 2]
- Swift 是通過協(xié)議擴(kuò)展提供默認(rèn)實(shí)現(xiàn)來到達(dá)可選的目的。但需要注意的是,標(biāo)記為 @objc 的 protocol 只能被 class 實(shí)現(xiàn),不能被 struct 和 enum 類型實(shí)現(xiàn),而且實(shí)現(xiàn)它的 class 中的方法也必須被標(biāo)注為 @objc,或者整個(gè)類就是繼承自 NSObject。
@objc protocol CounterDataSource {
@objc optional func incrementForCount(count: Int) -> Int
@objc optional var fixedIncrement: Int { get }
}