Static vs class functions/variables in Swift classes?

前端 未结 9 898
不知归路
不知归路 2020-11-28 17:13

The following code compiles in Swift 1.2:

class myClass {
    static func myMethod1() {
    }
    class func myMethod2() {
    }
    static var myVar1 = \"\"         


        
相关标签:
9条回答
  • 2020-11-28 17:50

    There's one more difference. class can be used to define type properties of computed type only. If you need a stored type property use static instead.

    "You define type properties with the static keyword. For computed type properties for class types, you can use the class keyword instead to allow subclasses to override the superclass’s implementation."

    0 讨论(0)
  • 2020-11-28 17:55

    Adding to above answers static methods are static dispatch means the compiler know which method will be executed at runtime as the static method can not be overridden while the class method can be a dynamic dispatch as subclass can override these.

    0 讨论(0)
  • 2020-11-28 18:03

    There's one more difference. class can be used to define type properties of computed type only. If you need a stored type property use static instead.

    Class :- reference type

    struct :- value type

    0 讨论(0)
  • 2020-11-28 18:06

    I got this confusion in one of my project as well and found this post, very helpful. Tried the same in my playground and here is the summary. Hope this helps someone with stored properties and functions of type static, final,class, overriding class vars etc.

    class Simple {
    
        init() {print("init method called in base")}
    
        class func one() {print("class - one()")}
    
        class func two() {print("class - two()")}
    
        static func staticOne() {print("staticOne()")}
    
        static func staticTwo() {print("staticTwo()")}
    
        final func yesFinal() {print("yesFinal()")}
    
        static var myStaticVar = "static var in base"
    
        //Class stored properties not yet supported in classes; did you mean 'static'?
        class var myClassVar1 = "class var1"
    
        //This works fine
        class var myClassVar: String {
           return "class var in base"
        }
    }
    
    class SubSimple: Simple {
        //Successful override
        override class func one() {
            print("subClass - one()")
        }
        //Successful override
        override class func two () {
            print("subClass - two()")
        }
    
        //Error: Class method overrides a 'final' class method
        override static func staticOne() {
    
        }
    
        //error: Instance method overrides a 'final' instance method
        override final func yesFinal() {
    
        }
    
        //Works fine
        override class var myClassVar: String {
            return "class var in subclass"
        }
    }
    

    And here is the testing samples:

    print(Simple.one())
    print(Simple.two())
    print(Simple.staticOne())
    print(Simple.staticTwo())
    print(Simple.yesFinal(Simple()))
    print(SubSimple.one())
    print(Simple.myStaticVar)
    print(Simple.myClassVar)
    print(SubSimple.myClassVar)
    
    //Output
    class - one()
    class - two()
    staticOne()
    staticTwo()
    init method called in base
    (Function)
    subClass - one()
    static var in base
    class var in base
    class var in subclass
    
    0 讨论(0)
  • 2020-11-28 18:07

    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.

    0 讨论(0)
  • 2020-11-28 18:08

    I tried mipadi's answer and comments on playground. And thought of sharing it. Here you go. I think mipadi's answer should be mark as accepted.

    class A{
        class func classFunction(){
        }
        static func staticFunction(){
        }
        class func classFunctionToBeMakeFinalInImmediateSubclass(){
        }
    }
    
    class B: A {
        override class func classFunction(){
    
        }
    
        //Compile Error. Class method overrides a 'final' class method
        override static func staticFunction(){
    
        }
    
        //Lets avoid the function called 'classFunctionToBeMakeFinalInImmediateSubclass' being overriden by subclasses
    
        /* First way of doing it
        override static func classFunctionToBeMakeFinalInImmediateSubclass(){
        }
        */
    
        // Second way of doing the same
        override final class func classFunctionToBeMakeFinalInImmediateSubclass(){
        }
    
        //To use static or final class is choice of style.
        //As mipadi suggests I would use. static at super class. and final class to cut off further overrides by a subclass
    }
    
    class C: B{
        //Compile Error. Class method overrides a 'final' class method
        override static func classFunctionToBeMakeFinalInImmediateSubclass(){
    
        }
    }
    
    0 讨论(0)
提交回复
热议问题