3

It was very convenient to have static variables in Objective C (static variable's value is maintained throughout all function/method calls), however I couldn't find anything like this in Swift.

Is there anything like this?

This is an example of 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;
}
Shmidt
  • 16,436
  • 18
  • 88
  • 136
  • possible duplicate of [Static properties in Swift](http://stackoverflow.com/questions/26567480/static-properties-in-swift) –  Aug 01 '15 at 05:09
  • @santiago It's not. Static keyword in Objective C and Swift have absolutely different meaning. – Shmidt Aug 01 '15 at 05:11

2 Answers2

5

After seeing your updated answer, here 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 is anywhere in your class

staticFunc() //Static Value of x: 1
staticFunc() //Static Value of x: 2
staticFunc() //Static Value of x: 3
Mick MacCallum
  • 129,200
  • 40
  • 280
  • 281
Sohil R. Memon
  • 9,404
  • 1
  • 31
  • 57
2

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

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

Reference from HERE.

UPDATE:

From your C example you can achieve same thing in swift this way:

var x = 0   //this is global variable 

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

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

Tested with playground.

From Apple Document:

In C and Objective-C, you define static constants and variables associated with a type as global static variables. In Swift, however, type properties are written as part of the type’s definition, within the type’s outer curly braces, and each type property is explicitly scoped to the type it supports.

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. The example below shows the syntax for stored and computed type properties:

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 computed type property examples above are for read-only computed type >properties, but you can also define read-write computed type properties with the same syntax as for computed instance properties.

Community
  • 1
  • 1
Dharmesh Kheni
  • 71,228
  • 33
  • 160
  • 165