gpt4 book ai didi

ios - 类是否可以显式调用协议(protocol)扩展的方法?

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:11:34 26 4
gpt4 key购买 nike

假设存在以下协议(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 已经实现了其自定义版本的(因此优先于默认值)。

我们首先查看您的示例的轻量级版本,其中为 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
}
}

我们在MyClass2someFuncWithDefaultImplementation()的自定义实现中使用了非优雅嵌套类型方案,调用后者的默认实现,只是存储了一个引用在 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com