Swift protocol defining class method returning self

大兔子大兔子 提交于 2019-12-21 14:01:33

问题


I had code that was working in XCode 6 beta but stopped working recently after updating to xcode 6.1.

This is my protocol:

protocol CanDeserialiseFromJson {
    class func FromJson(json : JSONValue) -> Self
}

This is implementation:

extension Invoice : CanDeserialiseFromJson {
    class func FromJson(json : JSONValue) -> Self {
        return Invoice()
    }
}

This fails giving error:

'Invoice' is not convertable to 'Self'

As I said, this used to work and I can't work out why it doesn't anymore


回答1:


Self in a protocol is a requirement that implementations of the protocol use their own type. Since Invoice is the type you're adopting the protocol in, your implementation of FromJson should have a return type of Invoice.




回答2:


It's right. Your method is declared to return Self, whereas you are returning Invoice. Class methods are inherited, and in subclasses, Self will be that subclass type, and Invoice is not a subtype of that type.

To actually return Self, assuming Invoice has a required init() constructor, you can do something like this:

extension Invoice : CanDeserialiseFromJson {
    class func FromJson(json : JSONValue) -> Self {
        return self()
    }
}



回答3:


In case you really need to return Self (in my case I have an Objective-C protocol that translates a method into swift function returning Self) and not mark your class as final, you need to create a required initializer and use that:

class Foo {
    static func bar() -> Self {
        return self.init()
    }

    required init() {

    }
}

The final/required requirement comes from the fact that you might subclass this class and have a different initialiser. Final removes the option for a subclass, while the required init makes sure that any subclass will implement the required initialiser used in the static method.



来源:https://stackoverflow.com/questions/26244732/swift-protocol-defining-class-method-returning-self

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!