关于 Objective-C 项目中使用的 extern 的 3 个问题
当我在方法或变量声明之前使用单词
extern
时,我是否将其设置为全局的,从而在整个项目中可读/可写/可用?如果我在关键字之前使用 extern,是否有可能我的项目的一部分仍然无法访问它?例如,仅通过子类..例如当我使用“protected”时。
extern
是一个 C 关键字,对吗? Objective-C 中有类似的东西吗?我其实不明白为什么他们在 Objective-C 项目中使用 C 关键字。
谢谢
When I use the word
extern
before a method or variable declaration, am I making it global and therefore readable/writable/usable over the entire project ?If I use extern before a keyword, is there any chance it is still not accessible by part of my project ? For example, only by subclasses.. such as when I use "protected".
extern
is a C keyword, right? Is there an equivalent in Objective-C? I actually don't understand why they use a C keyword in an Objective-C project.
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
1)您正在指定其链接。外部链接允许您或任何客户端引用该符号。
关于全局变量:如果变量是可变的和/或需要正确的构造,那么您应该考虑该对象的方法或函数。值得注意的例外是 NSString 常量:
2) extern 关键字不会影响可见性(public/protected/private/package)。要使用符号(例如常量或 C 函数),只需包含声明它的标头。
如果您是该语言的新手,可能会有些困惑:将 extern C 声明(常量、函数)放在
@interface 之间。 @end
不会改变其作用域:具有与以下相同的作用域(全局)和可见性(公共):
因此将与类相关的 C 常量或函数放在
@interface 中确实没有意义。 ..@end
和@implementation...@end
。我建议将它们放在与接口相同的标头中,在@interface/@end
和@implementation/@end
之外,并在名称前加上与其关联的类的前缀,像这样:如果您希望该常量是私有的,只需像这样声明和定义它:
不幸的是,没有同样简单或通用的方法来使该常量受 objc 保护。
最后,如果数量因类而异,您还可以使用类方法:
3)是的,除其他语言外。例如,如果您在 C++ 翻译中使用
extern const int Something
,则 C++ 翻译将查找声明为 extern C++ 符号的Something
。 objc 中没有替换; objc 是 C 的超集,继承了 C 的所有功能。extern
的使用格式良好,您也可以在您使用的框架(例如 Foundation)中找到它。他们使用它是因为他们需要指定链接。 objc 不提供替代品,大概是因为它不需要更换或扩展。为了避免这种情况,只需使用像这样的
#define
:1) you're specifying its linkage. extern linkage allows you or any client to reference the symbol.
regarding global variables: if the variable is mutable and/or needs proper construction, then you should consider methods or functions for this object. the notable exception to this is NSString constants:
2) there is no case where the extern keyword affects visibility (public/protected/private/package). to use the symbol (e.g. the constant or C function), simply include the header it is declared in.
somewhat confusing if you are new to the language: placing extern C declarations (constants, functions) in between
@interface ... @end
will not alter its scope:has the same scope (global) and visibility (public) as:
so it really makes no sense to place your class related C constants or functions in the
@interface...@end
and@implementation...@end
. i recommend placing these in the same header as the interface, outside@interface/@end
and@implementation/@end
and prefixing the name with the class it is associated with, like so:and if you want that constant to be private, just declare and define it like this:
unfortunately, there is no equally simple or common way to make this constant protected with objc.
finally, you can also use class methods if the number should vary by class:
3) yes, among other languages. for example, if you use
extern const int Something
in a c++ translation, the c++ translation will look forSomething
declared as an extern C++ symbol. there is no substitution in objc; objc is a superset of C and inherits all of C's functionalities. use ofextern
is well formed and you can also find it in the frameworks you use (e.g. Foundation). they use it because they need to specify linkage. objc does not offer a substitute, presumably because it did not require a replacement or extension.to avoid this, simply use a
#define
like this:extern
并不意味着“全局”,它意味着“在其他地方定义”。它用于告诉编译器存在变量或函数(在另一个目标文件或库中),以便编译器不会抱怨它,并且将向链接器提供该目标文件或库。因此
extern
意味着目标项是全局的。Objective-C 只是 C 的超集。C 中可用的所有内容在 Objective-C 中也可用,具有相同的语法和语义。 Objective-C 中没有以其他方式定义的 C 结构。
extern
doesn't mean "global", it means "defined elsewhere". It is used to tell the compiler that a variable or function exists (in another object file or library), so that it shall not complain about it and that the linker will be provided with that object file or library.As a consequence
extern
implies that the target item is global.Objective-C is just a superset of C. Everything that is available in C is available in Objective-C as well, with the same syntax and semantic. There is no construct of C that is defined in another way in Objective-C.
第 3 点:是的,您可以在 Objective C 中使用 FOUNDATION_EXPORT,它是一个宏,根据编译 C 还是 C++,解析为不同的关键字
。有关差异的更多信息,请参见此处:
“FOUNDATION_EXPORT”与“extern”
Point 3: Yes you can use FOUNDATION_EXPORT in objective C which is a macro that resolves to different keyword depending if compiling C or C++
More info here on the differences:
"FOUNDATION_EXPORT" vs "extern"