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


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


Login to comment


Swift protocol with "where Self" clause

Protocol func returning Self

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

Returning Self in Swift

Protocol for class method

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

Swift Protocol of a particular class

Swift Protocol Function: returning the same type of conforming class

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

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

Defining a javascript class Method as a constant

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

Class to protocol conversation in swift

Swift protocol delegate returning nil

Error for defining class method as expression

Swift protocol and a method returning "some View"

Conditionally defining method for container class

custom class method returning nil Swift IOS

Defining a class with a method

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

Swift Class Pointer as? Class Protocol?

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

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

defining method of class in if statement

Swift Protocol on a specific class?

Obj-C to Swift3 for class returning method

Cannot get data from "self" keyword from a class which is extended from a protocol in swift iOS?

Swift self.classForCoder in class method

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