Static vs class functions/variables in Swift classes?

前端 未结 9 899
不知归路
不知归路 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 18:12

    class vs static

    class is used inside Reference Type(class):

    • computed property
    • method
    • can be overridden by subclass

    static is used inside Reference Type and Value Type(class, enum):

    • computed property and stored property
    • method
    • cannot be changed by subclass
    protocol MyProtocol {
    //    class var protocolClassVariable : Int { get }//ERROR: Class properties are only allowed within classes
        static var protocolStaticVariable : Int { get }
        
    //    class func protocolClassFunc()//ERROR: Class methods are only allowed within classes
        static func protocolStaticFunc()
    }
    
    struct ValueTypeStruct: MyProtocol {
        //MyProtocol implementation begin
        static var protocolStaticVariable: Int = 1
        
        static func protocolStaticFunc() {
            
        }
        //MyProtocol implementation end
        
    //    class var classVariable = "classVariable"//ERROR: Class properties are only allowed within classes
        static var staticVariable = "staticVariable"
    
    //    class func classFunc() {} //ERROR: Class methods are only allowed within classes
        static func staticFunc() {}
    }
    
    class ReferenceTypeClass: MyProtocol {
        //MyProtocol implementation begin
        static var protocolStaticVariable: Int = 2
        
        static func protocolStaticFunc() {
            
        }
        //MyProtocol implementation end
        
        var variable = "variable"
    
    //    class var classStoredPropertyVariable = "classVariable"//ERROR: Class stored properties not supported in classes
    
        class var classComputedPropertyVariable: Int {
            get {
                return 1
            }
        }
    
        static var staticStoredPropertyVariable = "staticVariable"
    
        static var staticComputedPropertyVariable: Int {
            get {
                return 1
            }
        }
    
        class func classFunc() {}
        static func staticFunc() {}
    }
    
    final class FinalSubReferenceTypeClass: ReferenceTypeClass {
        override class var classComputedPropertyVariable: Int {
            get {
                return 2
            }
        }
        override class func classFunc() {}
    }
    
    //class SubFinalSubReferenceTypeClass: FinalSubReferenceTypeClass {}// ERROR: Inheritance from a final class
    
    

    [Reference vs Value Type]

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

    Testing in Swift 4 shows performance difference in simulator. I made a class with "class func" and struct with "static func" and ran them in test.

    static func is:

    • 20% faster without compiler optimization
    • 38% faster when optimization -whole-module-optimization is enabled.

    However, running the same code on iPhone 7 under iOS 10.3 shows exactly the same performance.

    Here is sample project in Swift 4 for Xcode 9 if you like to test yourself https://github.com/protyagov/StructVsClassPerformance

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

    Regarding to OOP, the answer is too simple:

    The subclasses can override class methods, but cannot override static methods.

    In addition to your post, if you want to declare a class variable (like you did class var myVar2 = ""), you should do it as follow:

    class var myVar2: String {
        return "whatever you want"
    }
    
    0 讨论(0)
提交回复
热议问题