How to declare volatile variables in Swift

后端 未结 4 2010
醉梦人生
醉梦人生 2021-02-19 07:57

I want to convert to Swift from Objective-C code like following;

int sum = 0;
x = 1;
for (int i = 0; i < 100; i++) {
    sum += x;
}

x is ac

4条回答
  •  孤独总比滥情好
    2021-02-19 08:22

    If you want to use x from anywhere, then you need to write this variable like this, var x = 0

    • Original code :

    int sum = 0; x = 1; for (int i = 0; i < 100; i++) { sum += x; }

    Updated code :

    var sum = 0 x = 1 for (i in 0..<100) { sum = sum + x }

    • var sum = 0 you can define this variable to anywhere in the class or outside too.
    • let sum = 0 you will not able to edit that value.

    Let us know if any confusion here.

提交回复
热议问题