【发布时间】:2012-03-05 12:54:32
【问题描述】:
好的,在 C、C++、C# 和 Objective-C 之间切换时,我仍然会重新调整,所以有时我会头晕目眩。然而,这一次,我对正确的方法感到更加困惑,因为我已经看到了在 Objective-C 中声明静态变量的至少三种不同的方法,如果你认为它只是 C 本身的超集,还有第四种方法。那么这些哪个是正确的呢?
附加问题
如果我们想共享一个独立变量(即不是静态类变量,而是一个刚刚在头文件中定义的变量),是否与在“C”中的方式相同(在头文件中带有“extern”? )
选项 A
Foo.h
@interface Foo : NSObject{
static int Laa;
}
@end
Foo.m
@implementation Foo
...
@end
选项 B
Foo.h
@interface Foo : NSObject{
}
@end
Foo.m
static int Laa; // <-- Outside of the implementation
@implementation Foo
...
@end
选项 C
Foo.h
@interface Foo : NSObject{
}
@end
Foo.m
int Laa; // <-- Note no word 'static' here like in 'Option B'
@implementation Foo
...
@end
选项 D
Foo.h
static int Laa;
@interface Foo : NSObject{
}
@end
Foo.m
@implementation Foo
...
@end
选项 E
Foo.h
@interface Foo : NSObject{
}
@end
Foo.m
@implementation Foo
static int Laa;
...
@end
奖金问题...
您是否必须使用 extern 这个词,还是仅在您使用 .c/.c++ 文件而不是 .m/.mm 文件时使用?
【问题讨论】:
标签: objective-c static-variables