关于 Objective-C 项目中使用的 extern 的 3 个问题

发布于 2024-12-03 10:41:48 字数 304 浏览 0 评论 0原文

  1. 当我在方法或变量声明之前使用单词 extern 时,我是否将其设置为全局的,从而在整个项目中可读/可写/可用?

  2. 如果我在关键字之前使用 extern,是否有可能我的项目的一部分仍然无法访问它?例如,仅通过子类..例如当我使用“protected”时。

  3. extern 是一个 C 关键字,对吗? Objective-C 中有类似的东西吗?我其实不明白为什么他们在 Objective-C 项目中使用 C 关键字。

谢谢

  1. 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 ?

  2. 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".

  3. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

欢你一世 2024-12-10 10:41:48

1)您正在指定其链接。外部链接允许您或任何客户端引用该符号。

关于全局变量:如果变量是可变的和/或需要正确的构造,那么您应该考虑该对象的方法或函数。值得注意的例外是 NSString 常量:

// MONClass.h
extern NSString* const MONClassDidCompleteRenderNotification;
// MONClass.m
NSString* const MONClassDidCompleteRenderNotification = @"MONClassDidCompleteRenderNotification";

2) extern 关键字不会影响可见性(public/protected/private/package)。要使用符号(例如常量或 C 函数),只需包含声明它的标头。

如果您是该语言的新手,可能会有些困惑:将 extern C 声明(常量、函数)放在 @interface 之间。 @end 不会改变其作用域:

@interface MONClass : NSObject

extern const size_t MaximumThreads;

@end

具有与以下相同的作用域(全局)和可见性(公共):

@interface MONClass : NSObject

@end

extern const size_t MaximumThreads;

因此将与类相关的 C 常量或函数放在 @interface 中确实没有意义。 ..@end@implementation...@end。我建议将它们放在与接口相同的标头中,在 @interface/@end@implementation/@end 之外,并在名称前加上与其关联的类的前缀,像这样:

@interface MONClass : NSObject

@end

extern const size_t MONClassMaximumThreads;
// MONClass.m
const size_t MONClassMaximumThreads = 23;

如果您希望该常量是私有的,只需像这样声明和定义它:

// MONClass.m
static const size_t MONClassMaximumThreads = 23;

@implementation MONClass

@end

不幸的是,没有同样简单或通用的方法来使该常量受 objc 保护。

最后,如果数量因类而异,您还可以使用类方法:

@interface MONMammal : NSObject
+ (NSUInteger)numberOfLegs;
@end

@implementation MONDog
+ (NSUInteger)numberOfLegs { return 4; }
@end
@implementation MONHuman
+ (NSUInteger)numberOfLegs { return 2; }
@end

3)是的,除其他语言外。例如,如果您在 C++ 翻译中使用 extern const int Something,则 C++ 翻译将查找声明为 extern C++ 符号的 Something。 objc 中没有替换; objc 是 C 的超集,继承了 C 的所有功能。 extern 的使用格式良好,您也可以在您使用的框架(例如 Foundation)中找到它。他们使用它是因为他们需要指定链接。 objc 不提供替代品,大概是因为它不需要更换或扩展。

为了避免这种情况,只需使用像这样的#define

#if !defined(__cplusplus)
#define MONExternC extern
#else
#define MONExternC extern "C"
#endif

MONExternC const size_t MONClassMaximumThreads;

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:

// MONClass.h
extern NSString* const MONClassDidCompleteRenderNotification;
// MONClass.m
NSString* const MONClassDidCompleteRenderNotification = @"MONClassDidCompleteRenderNotification";

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:

@interface MONClass : NSObject

extern const size_t MaximumThreads;

@end

has the same scope (global) and visibility (public) as:

@interface MONClass : NSObject

@end

extern const size_t MaximumThreads;

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:

@interface MONClass : NSObject

@end

extern const size_t MONClassMaximumThreads;
// MONClass.m
const size_t MONClassMaximumThreads = 23;

and if you want that constant to be private, just declare and define it like this:

// MONClass.m
static const size_t MONClassMaximumThreads = 23;

@implementation MONClass

@end

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:

@interface MONMammal : NSObject
+ (NSUInteger)numberOfLegs;
@end

@implementation MONDog
+ (NSUInteger)numberOfLegs { return 4; }
@end
@implementation MONHuman
+ (NSUInteger)numberOfLegs { return 2; }
@end

3) yes, among other languages. for example, if you use extern const int Something in a c++ translation, the c++ translation will look for Something 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 of extern 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:

#if !defined(__cplusplus)
#define MONExternC extern
#else
#define MONExternC extern "C"
#endif

MONExternC const size_t MONClassMaximumThreads;
风铃鹿 2024-12-10 10:41:48

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.

酸甜透明夹心 2024-12-10 10:41:48

第 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"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文