假设有以下协议(protocol),其中包含由扩展提供的 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在线程中
描述了使用嵌套的虚拟类型从已经提供了自己的蓝图方法实现的类型中访问协议(protocol)的默认实现。我们可以扩展此方法以允许实际使用 MyClass2
实例的(蓝图)属性,但在对 MyClass2
已经实现其自定义版本的默认实现的调用中of(因此优先于默认值)。
我们首先查看您的示例的轻量级版本,并为 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
}
}
我们在MyClass2
的someFuncWithDefaultImplementation()
的自定义实现中使用非优雅的嵌套类型解决方案,调用后者的默认实现,但是存储了一个引用在 Dummy
实例中返回 MyClass2
实例,以允许在 MyClass2
的 someInt
属性中使用默认实现调用(用于读取和写入),即使这是从 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!
}
}
但是,我将重复链接答案的作者:这种方法不是很优雅。
关于ios - 类是否可以显式调用协议(protocol)扩展上的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43354427/
欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://jike.in/) | Powered by Discuz! X3.4 |