在 Objective-C 中声明静态变量的正确方法是什么?
好吧,在 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 文件时才使用?
Ok, still re-adjusting to things when switching between C, C++, C# and Objective-C so sometimes my head spins. This time however, I'm more confused as to the proper way since I have seen at least three different ways to declare static variables in Objective-C, and there's a fourth if you consider it's just a superset of C itself. So which of these is right?
Additional Question
If we want to share a stand-alone variable (i.e. not a static class variable, but one just defined in a header) is that done the same way as in 'C' (ala with 'extern' in the header?)
Option A
Foo.h
@interface Foo : NSObject{
static int Laa;
}
@end
Foo.m
@implementation Foo
...
@end
Option B
Foo.h
@interface Foo : NSObject{
}
@end
Foo.m
static int Laa; // <-- Outside of the implementation
@implementation Foo
...
@end
Option C
Foo.h
@interface Foo : NSObject{
}
@end
Foo.m
int Laa; // <-- Note no word 'static' here like in 'Option B'
@implementation Foo
...
@end
Option D
Foo.h
static int Laa;
@interface Foo : NSObject{
}
@end
Foo.m
@implementation Foo
...
@end
Option E
Foo.h
@interface Foo : NSObject{
}
@end
Foo.m
@implementation Foo
static int Laa;
...
@end
Bonus question...
Do you have to use the word extern
or is that only when you are using .c/.c++ files, not .m/.mm files?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
选项A是错误的。 Objective -c 类没有静态变量。
选项B和E是实现静态变量的正确方法。
选项 C 创建一个全局变量,可以使用 extern 关键字在实现文件外部访问该变量。
选项 D 再次创建一个全局静态变量,只需导入 .h 文件即可从任何地方访问该变量。
关于你的额外问题:extern关键字与C/C++中的含义相同。
The Option A is wrong. Objective -c class doesn't have a static variable.
Option B and E are the correct way to implement static variables.
Option C creates a global variable that might be accessed out side the implementation file using extern keyword.
Option D again creates a global static variable which can be accessed from anywhere by just importing .h file.
About your bonus question: extern keyword has the same meaning as in C/C++.
这取决于你所说的“静态”是什么意思。 Objective-C 从 C 中获取 static 关键字,因此与类无关。如果你想像在 C++ 中那样创建一个类变量,那么 Objective-C等效项只是
.m
文件中的全局变量。如果您在声明全局时使用 static 关键字,它可以确保该变量不能被外部化并在其他文件中使用,这可能就是您所追求的。It depends on what you mean by "static". Objective-C gets the static keyword from C, so it has nothing to do with classes. If you're trying to make a class variable like you would in C++, then the Objective-C equivalent is just a global inside the
.m
file. If you use the static keyword when declaring the global, it ensures that the variable can't beextern
'd and used in other files, which is probably what you're after.由于您提供了多项选择,我必须选择选项 A。这是针对仅在类中可见的静态变量。为了从外部访问它,您需要编写一个自定义的 getter 和 setter 方法,类似于 这个答案。
Since you provided multiple choice, I'd have to go with Option A. This is for a static variable only visible within the class. For access to it externally, you'd write a custom getter and setter method similar to this answer.