在我的代码停止编译后,我能够将其分解为简单的问题:
当使用符合协议的associatedType
时,它再次符合AnyObject
,我的代码不再编译甚至没有给出适当的错误:
SwiftCompile命令失败,退出代码为非零
如何确保parent
属性符合实现符合AnyObject
的特定协议的任何对象(即是一个类)?
简单代码示例(MRE):
protocol ParentProtocol: AnyObject { // without AnyObject this runs fine
var value: Int { get set }
}
protocol ChildProtocol: ParentProtocol {
associatedtype parentType: ParentProtocol
var parent: parentType! { get set }
}
class Test {
var child: (any ChildProtocol)?
func call() {
if let child = child {
print(child.parent.value)
}
}
}
字符串
1条答案
按热度按时间u1ehiz5o1#
你真的需要
ParentProtocol.parentType
成为关联类型吗?您的代码编译为:
字符串