我试图弄清楚如何声明一个静态变量,只在本地作用于Swift中的函数.
在C中,这可能看起来像这样:
int foo() {
static int timesCalled = 0;
++timesCalled;
return timesCalled;
}
Run Code Online (Sandbox Code Playgroud)
在Objective-C中,它基本相同:
- (NSInteger)foo {
static NSInteger timesCalled = 0;
++timesCalled;
return timesCalled;
}
Run Code Online (Sandbox Code Playgroud)
但我似乎无法在Swift中做这样的事情.我试过用以下方式声明变量:
static var timesCalledA = 0
var static timesCalledB = 0
var timesCalledC: static Int = 0
var timesCalledD: Int static = 0
Run Code Online (Sandbox Code Playgroud)
但这些都会导致错误.
static)和"预期模式"(在哪里timesCalledB)static)和"预期类型"(在哪里static)Int和static)和"预期声明"(等号下签)