假设存在以下协议,并且扩展提供了someFuncWithDefaultImplementation()的默认实现。
那么MyClass2是否有可能同时提供someFuncWithDefaultImplementation()的自己的实现,该实现也从扩展中调用该方法的默认实现?
protocol MyProtocol : class {
func someFuncWithDefaultImplementation()
func someFunc()
var someInt:Int { get set }
}
extension MyProtocol {
func someFuncWithDefaultImplementation() {
someInt = 5
}
func someFunc() {
someFuncWithDefaultImplementation()
}
}
class MyClass : MyProtocol {
var someInt = 6
}
class MyClass2 : MyProtocol
{
var someInt: Int = 4
func someFuncWithDefaultImplementation()
{
// do some additional stuff
/*** someFuncWithDefaultImplementation() invoke MyProtocol extension implementation here ***/
}
}
let class1 = MyClass()
class1.someFunc()
let class2 = MyClass2()
class2.someFunc()
最佳答案
线程中的following answer
描述了使用嵌套虚拟类型从已经提供了自己的蓝图方法实现的类型中访问协议的默认实现。我们可以扩展此方法,以允许实际使用
MyClass2
实例的(蓝图)属性,但是在对默认实现的调用中,MyClass2
已经实现了其自定义版本(因此优先于默认实例)。我们首先查看示例的轻量级版本,并为
someFuncWithDefaultImplementation()
提供默认实现protocol MyProtocol : class {
func someFuncWithDefaultImplementation()
var someInt: Int { get set }
}
extension MyProtocol {
func someFuncWithDefaultImplementation() {
print("Called default impl. Currently, someInt = \(someInt)")
print("Mutates someInt from within default implementation (0) ...")
someInt = 0
}
}
我们在
someFuncWithDefaultImplementation()
的MyClass2
的自定义实现中使用了非优雅的嵌套类型解决方案,以调用后者的默认实现,但是将引用存储在Dummy
实例中,回到MyClass2
实例,以允许someInt
的MyClass2
属性为在默认实现调用(用于读取和写入)中使用,即使从Dummy
类型调用也是如此。class MyClass2 : MyProtocol
{
var someInt: Int = 42
func someFuncWithDefaultImplementation()
{
// do some additional stuff ...
print("In MyClass2 implementation, currently someInt = \(someInt)")
/* Dummy 'MyClass2'-capturing type used to call the default
implementation of 'MyProtocol', but with read and write
access to 'MyClass2':s self:s 'someInt' instance. */
class Dummy : MyProtocol {
unowned let myClass2: MyClass2
init(_ myClass2: MyClass2) { self.myClass2 = myClass2 }
var someInt: Int {
get { return myClass2.someInt }
set { myClass2.someInt = newValue }
}
}
// call default implementation of 'someFuncWithDefaultImplementation'
// supplying 'self' to read/write access to self.someInt.
Dummy(self).someFuncWithDefaultImplementation()
print("Back in MyClass2:s implementation; now someInt = \(someInt)")
// 0, woah, mutated in default implementation!
}
}
let a = MyClass2()
a.someFuncWithDefaultImplementation()
/* In MyClass2 implementation, currently someInt = 42
Called default impl. Currently, someInt = 42
Mutates someInt from within default implementation (0) ...
Back in MyClass2:s implementation; now someInt = 0 */
您还可以选择在函数外部声明嵌套的
Dummy
,只需将其标记为private
即可确保无法从MyClass2
外部进行访问:class MyClass2 : MyProtocol
{
var someInt: Int = 42
/* Dummy 'MyClass2'-capturing type used to call the default
implementation of 'MyProtocol', but with read and write
access to 'MyClass2':s self:s 'someInt' instance. */
private class Dummy : MyProtocol {
unowned let myClass2: MyClass2
init(_ myClass2: MyClass2) { self.myClass2 = myClass2 }
var someInt: Int {
get { return myClass2.someInt }
set { myClass2.someInt = newValue }
}
}
func someFuncWithDefaultImplementation()
{
// do some additional stuff ...
print("In MyClass2 implementation, currently someInt = \(someInt)")
// call default implementation of 'someFuncWithDefaultImplementation'
// supplying 'self' to read/write access to self.someInt.
Dummy(self).someFuncWithDefaultImplementation()
print("Back in MyClass2:s implementation; now someInt = \(someInt)")
// 0, woah, mutated in default implementation!
}
}
但是,我将重复与链接答案的作者相同的方法:这种方法不是很好。