14

I am not able to find any difference between class static function to struct static function. As I know class static function can not be inherited and struct does not have an option for inheritance.

Please do not get confused by static func and class func in class.

class a {
    static func myMethod1() {
    }
}

vs

struct a {
    static func myMethod1() {
    }
}
2
  • They are the same except with static then you cant override it, thats why it's used for struct, since struct also cant be subclassed Commented Mar 31, 2017 at 8:52
  • They are the same. Both are statically dispatched (compile-time) methods on a type. Commented Jun 3, 2018 at 13:00

3 Answers 3

11

This is kind of a stretch, but due to the reference vs value semantics of class and struct types, respectively, there is subtle difference in in the realization of a case where you want to make use of a type method (static) to mutate a private property of the type, given an instance of the type has been supplied. Again, kind of a stretch, as this focuses on differences in implementation details, and not on a concrete difference between the two.

In the class case, an immutable reference can be supplied to the static method, which in turn can be used to mutate a private instance member of the type. In the case of the struct, the instance of the type naturally needs to be supplied as an inout parameter, as changing the value of an instance member of a value type also means changing the value of the instance itself.

class A {
    private(set) var i: Int = 0
    static func foo(_ bar: A) { 
        bar.i = 42
    }
}

struct B {
    private(set) var i: Int = 0
    static func foo(_ bar: inout B) { 
        bar.i = 42  
    }
}

let a = A()
var b = B()

A.foo(a)
print(a.i) // 42
B.foo(&b)
print(b.i) // 42
Sign up to request clarification or add additional context in comments.

2 Comments

Although of course A's static func foo(_ bar: A) is basically (ignoring the ability to access the metatype value that it's called on) an equivalent of final func foo() and B's static func foo(_ bar: inout B) is basically an equivalent of mutating func foo() :) The only other difference I can think of is that by making them static and having an explicit parameter for the instance (rather than relying on the implicit self parameter), you gain the ability to overload them with different parameter types.
@Hamish yes, truly a stretch (and not entirely associated with static, but rather the inherent difference of value vs. reference types). Good point about the overloading possibility when being explicit!
-1

These link may help you to understand the functions. As @mipadi says (emphasis mine):

static and class both associate a method with a class, rather than an instance of a class. The difference is that subclasses can override class methods; they cannot override static methods.

class properties will theoretically function in the same way (subclasses can override them), but they're not possible in Swift yet.

What is the difference between static func and class func in Swift?

Static vs class functions/variables in Swift classes?

3 Comments

That's i know and you might have missed my question, it's about struct static func vs class static func, not about static func and class func.
Mark question as duplicate if you think it is duplicate
Please don't just quote another answer without adding any content of your own. Quoting is fine (as long as you give proper attribution), but you should use it to support your own ideas. If you feel a question is a duplicate, then please flag it as such (as it happens, it is not a duplicate – the answer you're quoting is talking about class methods vs. static methods, which the question is not about).
-1

The difference is that subclasses can override class methods but cannot override static methods.

Here is the simple example that is tested in the Playground

class A{
    class func classFunction(){
    }
    static func staticFunction(){
    }
}

class B: A {
    // override successfully
    override class func classFunction(){
    }

    //Compile Error. Cannot override static method
    override static func staticFunction(){
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.