How to declare volatile variables in Swift

后端 未结 4 2012
醉梦人生
醉梦人生 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:12

    I will expand on @potatoswatter's (excellent) comments . There are a couple of misunderstandings here

    • Volatile has nothing to do with accessibility
    • Volatile is not a sufficient construct here since it is ensures read consistency to threads when they access that variable. Your use case is involving mutations to x and thus requires synchronization that is a more thorough concurrency construct. This is a moderately advanced concept and is not in the wheelhouse of swift.

提交回复
热议问题