Protocol func returning Self

后端 未结 9 1896
悲哀的现实
悲哀的现实 2020-11-22 15:11

I have a protocol P that returns a copy of the object:

protocol P {
    func copy() -> Self
}

and a class C that implements P:



        
9条回答
  •  孤独总比滥情好
    2020-11-22 15:59

    With Swift 2, we can use protocol extensions for this.

    protocol Copyable {
        init(copy:Self)
    }
    
    extension Copyable {
        func copy() -> Self {
            return Self.init(copy: self)
        }
    }
    

提交回复
热议问题