What is the analogue of Objective C static variable in Swift?

It was very convenient to have static variables in Objective C (the value of a static variable is supported in all function / method calls ), however I could not find anything like this in Swift.

Is there anything like this?

This is an example of a static variable in C:

void func() {
    static int x = 0; 
    /* x is initialized only once across four calls of func() and
      the variable will get incremented four 
      times after these calls. The final value of x will be 4. */
    x++;
    printf("%d\n", x); // outputs the value of x
}

int main() { //int argc, char *argv[] inside the main is optional in the particular program
    func(); // prints 1
    func(); // prints 2
    func(); // prints 3
    func(); // prints 4
    return 0;
}

      

+3


source to share


2 answers


After looking at the updated answer, this is the modification for your Objective-C code:

func staticFunc() {    
    struct myStruct {
        static var x = 0
    }

    myStruct.x++
    println("Static Value of x: \(myStruct.x)");
}

      



Call anywhere in your class

staticFunc() //Static Value of x: 1
staticFunc() //Static Value of x: 2
staticFunc() //Static Value of x: 3

      

+4


source


Declare a variable at the top level of the file (outside of any classes) called a global variable.

variables at the top level of the file are lazy initialized! So you can set a default value for your variable as the result of reading the file, and the file won't actually be read until your code first asks for the value of the variable.

Link from HERE .

UPDATE:

From your example, for example, you can achieve this just as quickly:

var x = 0   //this is global variable 

func staticVar() {
    x++
    println(x)
}

staticVar()
x                //1
staticVar()
x                //2
staticVar()
x                //3

      



Playground tested.

From Apple doc :

In C and Objective-C, you define static constants and type-bound variables as global static variables. In Swift, however, type properties are written as part of the type definition, inside the types of outer curly braces, and each type property is explicitly up to the level it supports.

You define properties of a type with the static keyword. For a computed property type for class types, you can use the class keyword instead of allowing subclasses to override the superclass's implementation. the example below shows the syntax for a stored and computed property type:

struct SomeStructure {
static var storedTypeProperty = "Some value."
static var computedTypeProperty: Int {
    // return an Int value here
    }
}
enum SomeEnumeration {
    static var storedTypeProperty = "Some value."
    static var computedTypeProperty: Int {
        // return an Int value here
    }
}
class SomeClass {
    static var storedTypeProperty = "Some value."
    static var computedTypeProperty: Int {
        // return an Int value here
    }
    class var overrideableComputedTypeProperty: Int {
        // return an Int value here
    }
}

      

NOTE

The above computed type property examples are only for> read-only properties, but you can also define a read-write type computation with the same syntax as for a computed property instance.

+2


source







All Articles