这段代码会产生 Xcode 错误消息,让您陷入困境。假设我有一个名为Marker
我希望标记能够复制自己。这是第一个猜测......
protocol Marker {
func copy() -> Self
}
class Marker1 : Marker {
func copy() -> Self {
return Marker1() // error here
}
}
(我不知道如何使用Self
正确,因为我在Swift 编程语言文档。如果您知道记录在哪里,请在答案中包含该内容。)
该代码在标记行上给出了错误:Cannot convert return expression of type 'Marker1' to return type 'Self'
它建议修复:Insert ' as! Self'
.
我接受修复:
...
return Marker1() as! Self
...
这会导致另一个编译器错误:'Self' is only available in a protocol or as the result of a method in a class; did you mean 'Marker1'?
如果我接受“修复”,它将返回到原始错误。我将其称为 Xcode 中的错误。让我们尝试一下其他的东西:
func copy() -> Marker1 {
return Marker1()
}
另一个错误:Method 'copy()' in non-final class 'Marker1' must return `Self` to conform to protocol 'Marker'
上课final
确实修复了错误。但有没有办法在不进行期末考试的情况下做到这一点呢?以及在哪里Self
有记录吗?