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() {
    }
}
George Green
  • 4,807
  • 5
  • 31
  • 45
Retro
  • 3,985
  • 2
  • 17
  • 41
  • 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 – Tj3n Mar 31 '17 at 08:52
  • They are the same. Both are statically dispatched (compile-time) methods on a type. – Sulthan Jun 03 '18 at 13:00

3 Answers3

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
dfrib
  • 70,367
  • 12
  • 127
  • 192
  • 1
    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 Mar 31 '17 at 13:10
  • @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! – dfrib Mar 31 '17 at 20:09
-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?

Community
  • 1
  • 1
Ashwin Kanjariya
  • 1,019
  • 2
  • 10
  • 22
  • 4
    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. – Retro Mar 31 '17 at 08:48
  • 1
    Mark question as duplicate if you think it is duplicate – Nirav D Mar 31 '17 at 09:21
  • 5
    Please don't just quote another answer without adding any content of your own. Quoting is fine (as long as you [give proper attribution](http://stackoverflow.com/help/referencing)), 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). – Hamish Mar 31 '17 at 09:24
-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(){
    }
}
Niraj Paul
  • 1,498
  • 14
  • 22