Swift protocol defining class method returning self

Stanislaw Wozniak

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

rickster

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Swift protocol and a method returning "some View"

'Self' is only available in a protocol or as the result of a class method

Not able to pass self (which implements a protocol) to init method of a class instantiation.(Swift)

Protocol func returning Self

Implementing protocol static method that takes and returns Self (in swift)

Method in non-final class must return `Self` to conform to protocol

Why does Swift disallow assignment to self in class init, but not in protocol init?

How to use a self-referencing Swift protocol as a type for a generic class

Swift self.classForCoder in class method

Swift Protocol Function: returning the same type of conforming class

custom class method returning nil Swift IOS

mypy: How to declare the return type of a method returning self in a generic class?

Returning Self in Swift

Defining a class with a method

defining method of class in if statement

Swift protocol with "where Self" clause

Swift protocol delegate returning nil

"Type of a class which conforms to a protocol" as parameter in a method swift

@obj protocol delegate method not working in second class. Swift

Protocol for class method

Swift: Assign a class method to a handler and handling weak self

Obj-C to Swift3 for class returning method

Conditionally defining method for container class

Defining a javascript class Method as a constant

Error for defining class method as expression

Class to protocol conversation in swift

Swift Protocol of a particular class

Swift Protocol on a specific class?

Creating class for defining constants in swift