gpt4 book ai didi

swift - 非最终类无法满足协议(protocol)要求,因为它在非参数、非结果类型中使用 'Self'

转载 作者:搜寻专家 更新时间:2023-10-31 08:31:45 25 4
gpt4 key购买 nike

<分区>

我想用一个类方法实现一个协议(protocol),该类方法将实现类的数组作为参数。例如这样的事情:

protocol MyProtocol {
static func foo(verticies: [Self]) -> Int
}

class MyClass : MyProtocol {
class func foo(verticies: [MyClass]) -> Int {
return 42
}
}

当我尝试这样做时,出现以下错误:

Protocol 'MyProtocol' requirement 'foo()' cannot be satisfied by a non-final 'MyClass' class because it uses 'Self' in a non-parameter, non-result type

但是如果我使用类型为 MyClass 的对象而不是数组,这将非常有效:

protocol MyProtocol {
static func foo(verticies: Self) -> Int
}

class MyClass : MyProtocol {
class func foo(verticies: MyClass) -> Int {
return 42
}
}

为什么这对数组类型不起作用,是否有正确的方法来做到这一点?

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