swift备忘录之protocols

格式

protocol SomeProtocol {
    // protocol definition goes here
}

如果类有superclass,则将superclass写在类后,然后接上protocol。

class SomeClass: SomeSuperclass, FirstProtocol, AnotherProtocol {
    // class definition goes here
}

property in protocol

property在protocol中只定义类型和名字,并不规定是stored property还是computed property。

protocol中的property都是以var定义。

protocol SomeProtocol {
    var mustBeSettable: Int { get set }
    var doesNotNeedToBeSettable: Int { get }
}

如果在protocol中定义为gettable和settable,那么实现的时候就不能定义成一个constant stored property或者一个read-only property。如果protocol中定义的是gettable,那么就实现的时候可以根据自己需要决定是否定义成也settable的property。

type property都需要以static关键字定义。

protocol AnotherProtocol {
    static var someTypeProperty: Int { get set }
}

method in protocol

instance method 和 type method都可以在protocol中定义,type method定义的时候需要以static修饰。

// type method
protocol SomeProtocol {
    static func someTypeMethod()
}

// instance method 
protocol RandomNumberGenerator {
    func random() -> Double
}

如果需要在protocol的method中改变instance的值,需要用mutating修饰。例如:

protocol Togglable {
    mutating func toggle()
}

如果你把一个protocol instance method标记为mutating,则在class中实现的时候就没必要加mutating关键字了,但是enumration和structure还是需要加。例如:

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 is now equal to .on

initializer in protocol

定义的格式为:

protocol SomeProtocol {
    init(someParameter: Int)
}

实现这个protocol的时候,需要在init前加required关键字。

class SomeClass: SomeProtocol {
    required init(someParameter: Int) {
        // initializer implementation goes here
    }
}

由于final修饰的class是不可以被继承的,所以final修饰的class在实现init方法的时候不需要加required。

如果子类在复写一个父类的init方法的时候,恰巧这个init是从protocol中实现的,则需要使用required和override两个关键字修饰。

protocol SomeProtocol {
    init()
}
 
class SomeSuperClass {
    init() {
        // initializer implementation goes here
    }
}
 
class SomeSubClass: SomeSuperClass, SomeProtocol {
    // "required" from SomeProtocol conformance; "override" from SomeSuperClass
    required override init() {
        // initializer implementation goes here
    }
}

protocols as types

由于protocol也是一种类型,所以protocol可以和其他类型一样用在很多地方,比如:

  1. 可以作为参数类型或者function、method、initializer的返回值。

  2. 可以作为一个常量、变量或者property。

  3. 可以作为Array、dictionary或其他容器类型的内容。

例如:

class Dice {
    let sides: Int
    let generator: RandomNumberGenerator
    init(sides: Int, generator: RandomNumberGenerator) {
        self.sides = sides
        self.generator = generator
    }
    func roll() -> Int {
        return Int(generator.random() * Double(sides)) + 1
    }
}

可以给generator赋值给任意实现了RandomNumberGenerator的instance。

protocol还可以用作代理,这点和oc中差不多,就不在多说了。

在class、enum或structure的extension中实现protocol


protocol TextRepresentable {
    var textualDescription: String { get }
}

extension Dice: TextRepresentable {
    var textualDescription: String {
        return "A \(sides)-sided dice"
    }
}

如果一个class、enumeration或者structure已经实现了一个protocol的所有功能,只是没有显示的实现这个protocol,这时可以用一个empty extension来使其adopt这个protocol。

struct Hamster {
    var name: String
    var textualDescription: String {
        return "A hamster named \(name)"
    }
}
extension Hamster: TextRepresentable {}

protocol type的collection

上面说过protocol也是一种类型而且可以存储在collection中,所以可以创建protocol类型的collection。

let things: [TextRepresentable] = [game, d12, simonTheHamster]

protocol inheritance

protocol可以被继承,而且可以多继承。

protocol InheritingProtocol: SomeProtocol, AnotherProtocol {
    // protocol definition goes here
}

class-only protocols

添加class关键字到protocol继承列表的最前面,可以限制protocol只被class实现,而不被enumeration和structure实现。如下:

protocol SomeClassOnlyProtocol: class, SomeInheritedProtocol {
    // class-only protocol definition goes here
}

protocol composition

可以设定一个type同时遵守多个protocol,通过&符号将他们连接起来,例如:

protocol Named {
    var name: String { get }
}
protocol Aged {
    var age: Int { get }
}
struct Person: Named, Aged {
    var name: String
    var age: Int
}
func wishHappyBirthday(to celebrator: Named & Aged) {
    print("Happy birthday, \(celebrator.name), you're \(celebrator.age)!")
}
let birthdayPerson = Person(name: "Malcolm", age: 21)
wishHappyBirthday(to: birthdayPerson)
// Prints "Happy birthday, Malcolm, you're 21!"

protocol composition并没有创建一个新的、永久性的protocol,而只是定义了一个临时性的protocol把所有的requirement组合到一个protocol中。

checking for protocol conformance

根据前面的type cast中的介绍,这里同样有is、as?、as!来作类型检测和转换,用法和之前一致,就不在介绍了。

optional protocol requirements

protocol可以定义为optioal,需要在被定义为optional的内容前面加关键字optional,估计应该是为了与oc兼容。格式如下:

@objc protocol CounterDataSource {
    @objc optional func increment(forCount count: Int) -> Int
    @objc optional var fixedIncrement: Int { get }
}

摘录来自: Apple Inc. “The Swift Programming Language (Swift 3)”。 iBooks. 

可以看到protocol定义前面和每个optional的method前面都用@objc修饰。

protocol extension

可以为protocol本身定义extension。格式为:

extension RandomNumberGenerator {
    func randomBool() -> Bool {
        return random() > 0.5
    }
}

通过extension,我们可以为protocol中的computed property和method定义默认的实现,如果实现这个protocol的Type本身提供了自己的实现,则会覆盖默认的实现。

extension PrettyTextRepresentable  {
    var prettyTextualDescription: String {
        return textualDescription
    }
}

我们还可以给protocol的extension添加条件约束,只有符合条件的conforming type才可以使用extension。例如:

extension Collection where Iterator.Element: TextRepresentable {
    var textualDescription: String {
        let itemsAsText = self.map { $0.textualDescription }
        return "[" + itemsAsText.joined(separator: ", ") + "]"
    }
}

使用如下:

let murrayTheHamster = Hamster(name: "Murray")
let morganTheHamster = Hamster(name: "Morgan")
let mauriceTheHamster = Hamster(name: "Maurice")
let hamsters = [murrayTheHamster, morganTheHamster, mauriceTheHamster]

//使用
print(hamsters.textualDescription)
// Prints "[A hamster named Murray, A hamster named Morgan, A hamster named Maurice]

如果一个confirming type对同一个method和property符合多个extensions约束,则swift会自动从这些extension中挑选一个最合适的选线。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,117评论 4 360
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 66,963评论 1 290
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 107,897评论 0 240
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,805评论 0 203
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,208评论 3 286
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,535评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,797评论 2 311
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,493评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,215评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,477评论 2 244
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,988评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,325评论 2 252
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,971评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,055评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,807评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,544评论 2 271
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,455评论 2 266

推荐阅读更多精彩内容