在Swift协议中 - 为什么我被迫使用关键字' Final'在我的羊班之前?

时间:2016-11-26 18:15:47

标签: ios swift protocols

protocol Cloner {
    func cloneSelf(thingBeingCloned t: Self) -> Self
}

final class Sheep : Cloner {
    func cloneSelf(thingBeingCloned t: Sheep) -> Sheep {
        let newSheep = Sheep.init()
        return newSheep
    }
}

如果我使用关键字' final'在课前羊离开时,编译器对我大吼大叫。我收到错误:

method 'cloneSelf(thingBeingCloned:)' in non-final class 'Sheep' must return `Self` to conform to protocol 'Cloner'

好的..然后我听到了,我将返回值更改为Self而不是Sheep,然后我又得到了另一个错误:

error: cannot convert return expression of type 'Sheep' to return type 'Self'

0 个答案:

没有答案