什么时候在Swift中使用静态常量和变量?

时间:2016-06-08 11:26:52

标签: ios swift variables static constants

有一些帖子介绍了如何在Swift中为static constantstatic variable编写代码。但目前尚不清楚何时使用static constantstatic variable而不是constantvariable。有人可以解释一下吗?

4 个答案:

答案 0 :(得分:66)

当您将静态var / let定义为类(或结构)时,该信息将在所有实例(或值)之间共享。

分享信息

class Animal {
    static var nums = 0

    init() {
        Animal.nums += 1
    }
}

let dog = Animal()
Animal.nums // 1
let cat = Animal()
Animal.nums // 2

正如您在此处所看到的,我创建了2个Animal的单独实例,但两者都共享相同的静态变量nums

的Singleton

通常使用静态常量来采用Singleton模式。在这种情况下,我们希望分配不超过1个类的实例。 为此,我们将对共享实例的引用保存在常量中,并隐藏初始化器。

class Singleton {
    static let sharedInstance = Singleton()

    private init() { }

    func doSomething() { }
}

现在,当我们需要Singleton实例时,我们会写

Singleton.sharedInstance.doSomething()
Singleton.sharedInstance.doSomething()
Singleton.sharedInstance.doSomething()

这种方法确实允许我们始终使用相同的实例,即使在应用程序的不同点。

答案 1 :(得分:10)

如何在Swift中编写静态常量和静态变量的代码。但目前尚不清楚何时使用静态常量和静态变量而不是常量和变量。谁能解释一下? 当您将静态var / let定义为类(或结构)时,该值将在所有实例(或值)之间共享。

静态变量/类是可以访问的变量,无需创建任何实例/对象。

class Human {
    static let numberOfEyes = 2 //human have only 2 eyes
    static var eyeDefect = false //whether human have side-effect or not. he can have defect later so its variable

    //other variables and functions
}

//you can access numberOfEyes like below no object of Human is created
print(Human.numberOfEyes)
print(Human.eyeDefect)

//Object of Human
let john = Human()

我想你知道常量和变量之间的区别。简而言之,不变的是它的价值永远不变;上面例子中的numberOfEyes和变量是其值改变的值;以上示例中的eyeDefect。

静态常量或变量放在内存(RAM)中,然后分离对象。即numberOfEyes分配的内存空间与John对象不同,不在John内部。

现在,何时使用静态常量/变量:

  1. 使用单例设计模式时:static let sharedInstance = APIManager()

    class APIManager(){
        static let sharedInstance = APIManager()
        //Your other variables/functions here below
    }
    //Use it as to get singleton instance of APIManager from anywhere in your application
    let instanceOfAPIManager = APIManager.sharedInstance
    
  2. 当你需要全局相同的任何值时,不需要创建类的实例,就像人类中的numberOfEyes一样。

  3. 由于内存问题,不建议使用静态变量/常量,因为一旦实例化/分配,它将保留在内存中,直到应用程序从内存中删除。我发现到目前为止使用静态变量/常量的最佳位置只是在制作单例模式时,有时指针用于其他正常变量和常量不使用静态因为:内存问题,在代码中运行单元测试将很困难使用静态变量/常量。不建议像Human类一样使用。而是将它们用作常量或变量,并通过制作实例来访问它们。

    class Human {
     let numberOfEyes = 2 //human have only 2 eyes
     var eyeDefect = false //whether human have side-effect or not. he can have defect later so its variable
    
       //other variables and functions
     }
    
    //you can access numberOfEyes like below if you need just those values.
    print(Human().numberOfEyes)
    print(Human().eyeDefect)
    

答案 2 :(得分:3)

静态常量和变量确实属于类本身,而不属于特定实例。一个类也可以有静态方法,可以在不创建类实例的情况下调用它。

因此,当您的班级MyClass包含静态变量x时,您也可以直接通过MyClass.x访问它。 x将在类的所有实例之间共享

答案 3 :(得分:0)

这更是一个重要的评论:

class Person {
    static var name = "Static John" // a property of Person 'type'
    var name = "Alex" // a property of Person 'instance'

    var nonStaticName = "Peter"
    static var staticName = "Sara"



    static func statFunc() {
        let x = Person.name // Static John
        let y = name // Static John or Alex?!  Static John!!!!
        let r = staticName // Sara
        let k = nonStaticName // ERROR: instance member 'nonStaticName' cannot be used on type 'Person'
        // The compiler is like: I'm referrting to the `nonStaticName` property of which instance?! There is no instance! Sorry can't do!

    }

    func nonStaticFunc() {
        let x = Person.name // Static John
        let y = name // Static John or Alex?! Alex!!! Because we're in a instance scope...
        let k = nonStaticName // Obviously works
        let r = staticName // ERROR: static member 'staticName' cannot be used on instance of type 'Person'. Person.staticName will work
    }
}

有趣的观察:

第一

static var name = "Static John" // a property of Person 'type'
var name = "Alex" // a property of Person 'instance'

不产生冲突。

第二:

您永远不能在静态变量内使用实例变量。您可以在实例函数 (如果)中使用静态变量,只要您在其前面加上类型前缀即可,例如do Person.name,而

可以在静态函数内部访问

静态变量,无论该类型是否带有前缀Person.staticNamestaticName都可以使用。