【发布时间】:2015-08-01 05:07:46
【问题描述】:
在 Objective C 中使用静态变量非常方便(静态变量的值在所有函数/方法调用中都保持不变),但是我在 Swift 中找不到这样的东西。
有这样的吗?
这是 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;
}
【问题讨论】:
-
@santiago 不是。 Objective C 和 Swift 中的 static 关键字的含义完全不同。
标签: swift