以下代码在Swift 1.2中编译:

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

func doSomething() {
    myClass.myMethod1()
    myClass.myMethod2()
    myClass.myVar1 = "abc"
}

静态函数和类函数的区别是什么?我应该用哪一个,什么时候用?

如果我尝试定义另一个变量类var myVar2 = "",它会说:

类中还不支持类存储属性;你是说“静电”吗?

当支持此特性时,静态变量和类变量之间的区别是什么(即当两者都在类中定义时)?我应该用哪一个,什么时候用?

(Xcode 6.3)


当前回答

Static和class都将方法与类关联,而不是类的实例。区别在于子类可以重写类方法;它们不能覆盖静态方法。

类属性以同样的方式起作用(子类可以重写它们)。

其他回答

Static和class都将方法与类关联,而不是类的实例。区别在于子类可以重写类方法;它们不能覆盖静态方法。

类属性以同样的方式起作用(子类可以重写它们)。

在Swift 4中的测试显示了模拟器的性能差异。我用“class func”创建了一个类,用“static func”创建了一个结构,并在测试中运行它们。

静态函数是:

在没有编译器优化的情况下,速度快20% 启用优化-全模块优化时,速度加快38%。

然而,在iOS 10.3下的iPhone 7上运行相同的代码显示出完全相同的性能。

如果你喜欢自己测试的话,这里是Swift 4的Xcode 9的示例项目 https://github.com/protyagov/StructVsClassPerformance

Swift类vs静态类

[参考与值类型]

class在引用类型(类,函数)中使用:

计算属性 方法 可以被子类覆盖吗

static用于引用类型(类,函数)和值类型(结构体,enum,元组):

计算属性和存储属性 方法 不能通过子类更改

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

我在操场上尝试了米帕迪的回答和评论。我想分享它。给你。我认为米帕迪的回答应该被认可。

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(){
        
    }
    
    //Let's 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(){
        
    }
}

添加到上述答案的静态方法是静态分派意味着编译器知道哪个方法将在运行时执行,因为静态方法不能被覆盖,而类方法可以是动态分派,因为子类可以覆盖这些方法。