Rectangle 27 0

ios Make swift class conform to protocol at staticclass level?


protocol InstanceVsStatic {
    func someInstanceFunc()
    static func someStaticFunc()
}

enum MyConformingEnum: InstanceVsStatic {
    case someCase

    static func someStaticFunc() {
           // code
    }
    func someInstanceFunc() {
        // code
    }
}

class MyConformingClass: InstanceVsStatic {
    class func someStaticFunc() {
           // code
    }
    func someInstanceFunc() {
        // code
    }
}

struct MyConformingStruct: InstanceVsStatic {
    static func someStaticFunc() {
           // code
    }
    func someInstanceFunc() {
        // code
    }
}
struct MyConformingStruct: InstanceVsStatic {
    static func doStuffStatically(){
        // code
    }

    static func someStaticFunc() {
           // code
    }

    func someInstanceFunc() {
        MyConformingStruct.doStuffStatically()
    }
}

UPDATED for Swift Version 2.0 and above

You can have an instance method call a static/class method:

As per Gregzo's answer, Swift 2.0+ allows methods to be declared as static in the protocol definition. These must be satisfied with static/class methods in objects that implement the protocol.

If you want to try this just use the keyword "static" in your protocol definition for methods you will implement as static or class methods in your conforming objects:

Other than indirectly as above, there is no way to use static (class) methods to conform to a protocol in pure swift version 1.2 and below. It is a known bug / unimplemented feature: https://openradar.appspot.com/20119848

Wondering if there is possible will be a feature in the future?

You cannot satisfy a protocol definition for a instance method with a static method or vice-versa, which makes this an incomplete answer for the question above.

Note
Rectangle 27 0

ios Make swift class conform to protocol at staticclass level?


protocol MixedProtocol
{
    static func staticFoo()
    func instanceBar()
}

class ExampleClass : MixedProtocol
{
    // implementing static func as class func is fine.
    // class funcs are overridable, not static ones
    class func staticFoo()
    {
        println( "I'm a class func" )
    }

    func instanceBar()
    {
        println( "I'm an instance func" )
    }
}

If a protocol func is static, the implementer should implement it as a static method, and if it isn't, as an instance method:

There's no straight way around it: conforming to a protocol means just that, and 'static' is very much a key feature of a protocol member declaration that implementers must respect.

Note
Rectangle 27 0

ios Make swift class conform to protocol at staticclass level?


protocol InstanceVsStatic {
    func someInstanceFunc()
    static func someStaticFunc()
}

enum MyConformingEnum: InstanceVsStatic {
    case someCase

    static func someStaticFunc() {
           // code
    }
    func someInstanceFunc() {
        // code
    }
}

class MyConformingClass: InstanceVsStatic {
    class func someStaticFunc() {
           // code
    }
    func someInstanceFunc() {
        // code
    }
}

struct MyConformingStruct: InstanceVsStatic {
    static func someStaticFunc() {
           // code
    }
    func someInstanceFunc() {
        // code
    }
}
struct MyConformingStruct: InstanceVsStatic {
    static func doStuffStatically(){
        // code
    }

    static func someStaticFunc() {
           // code
    }

    func someInstanceFunc() {
        MyConformingStruct.doStuffStatically()
    }
}

UPDATED for Swift Version 2.0 and above

You can have an instance method call a static/class method:

As per Gregzo's answer, Swift 2.0+ allows methods to be declared as static in the protocol definition. These must be satisfied with static/class methods in objects that implement the protocol.

If you want to try this just use the keyword "static" in your protocol definition for methods you will implement as static or class methods in your conforming objects:

Other than indirectly as above, there is no way to use static (class) methods to conform to a protocol in pure swift version 1.2 and below. It is a known bug / unimplemented feature: https://openradar.appspot.com/20119848

Wondering if there is possible will be a feature in the future?

You cannot satisfy a protocol definition for a instance method with a static method or vice-versa, which makes this an incomplete answer for the question above.

Note
Rectangle 27 0

ios Make swift class conform to protocol at staticclass level?


protocol MixedProtocol
{
    static func staticFoo()
    func instanceBar()
}

class ExampleClass : MixedProtocol
{
    // implementing static func as class func is fine.
    // class funcs are overridable, not static ones
    class func staticFoo()
    {
        println( "I'm a class func" )
    }

    func instanceBar()
    {
        println( "I'm an instance func" )
    }
}

If a protocol func is static, the implementer should implement it as a static method, and if it isn't, as an instance method:

There's no straight way around it: conforming to a protocol means just that, and 'static' is very much a key feature of a protocol member declaration that implementers must respect.

Note