objective-c 在目标 C 中使用 extern
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1946296/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Use of extern in Objective C
提问by Gaurav Ghate
How good is it to use extern in Objective C? It does make coding for some parts easy.. but doesn't it spoil the object orientation?
在 Objective C 中使用 extern 有多好?它确实使某些部分的编码变得容易......但它不会破坏对象方向吗?
回答by Quinn Taylor
You'll find that externis used extensively in the Cocoa frameworks, and one would be hard-pressed to find a convincing argument that their OO is "spoiled". On the contrary, Cocoa is well-encapsulated and only exposes what it must, often via extern. Globally-defined constants are certainly the most common usage, but not necessarily the only valid use.
您会发现它extern在 Cocoa 框架中被广泛使用,并且很难找到一个令人信服的论点,即他们的 OO 被“破坏”了。相反,Cocoa 被很好地封装并且只暴露它必须的东西,通常是通过 extern。全局定义的常量当然是最常见的用法,但不一定是唯一有效的用法。
IMO, using externdoesn't necessarily "spoil" object orientation. Even in OO, it is frequent to use variables that are accessible from anywhere. Using externis the most frequent workaround for the lack of "class variables" (like those declared with staticin Java) in Objective-C. It allows you to expand the scope in which you can reference a symbol beyond the compilation unit where it is declared, essentially by promising that it will be defined somewhere by someone.
IMO,使用extern不一定会“破坏”对象方向。即使在 OO 中,也经常使用可从任何地方访问的变量。extern对于staticObjective-C 中缺少“类变量”(如在 Java 中声明的那些),使用是最常见的解决方法。它允许您扩展可以在声明符号的编译单元之外引用符号的范围,主要是通过承诺将在某处由某人定义。
You can also combine externwith __attribute__((visibility("hidden")))to create a symbol that can be used outside its compilation unit, but not outside its linkage unit, so to speak. I've used this for custom library and framework code to properly encapsulate higher-level internal details.
您还可以结合extern与__attribute__((visibility("hidden")))创建一个符号,该符号可以在其编译单元之外使用,但不能在其链接单元之外使用,可以这么说。我已经将它用于自定义库和框架代码,以正确封装更高级别的内部细节。
回答by Thomas Zoechling
There are some use cases for the externkeyword in Objective-C.
Aaron Hillegass suggests to create global notification names extern.
e.g.:
extern在 Objective-C 中有一些关键字的用例。
Aaron Hillegass 建议创建全局通知名称 extern。例如:
extern NSString* const XYYourNotification;
You then define the actual NSString*in your implementation
然后NSString*在您的实现中定义实际
回答by Vladimir
回答by Sergiu Todirascu
Another example of a problem when not using extern:
不使用时出现问题的另一个示例extern:
Say you have a global variable in a header file:
假设您在头文件中有一个全局变量:
NSString *globalVar = @"Wonderful";
And you use it in 3 places by importing that header file. You're code won't compile, the linker complaining that you have 3 duplicate symbols defined in your code. To solve it you have two ways out:
您可以通过导入该头文件在 3 个地方使用它。您的代码无法编译,链接器抱怨您的代码中定义了 3 个重复的符号。要解决它,您有两种方法:
Use static, in which case each file that imports that header will have its separate reference defined (and changing one string won't affect the other strings imported in other files):
使用static,在这种情况下,导入该标头的每个文件都将定义其单独的引用(并且更改一个字符串不会影响其他文件中导入的其他字符串):
static NSString *globalVar = @"Wonderful";
Use externin the .h file and define it in the .m file. This way there will only be one reference defined, and each file will use that same reference (changes being reflected in all files):
用extern在.h文件和.m文件定义它。这样只会定义一个引用,并且每个文件都将使用相同的引用(更改会反映在所有文件中):
extern NSString *globalVar; // in .h
NSString *globalVar = @"Wonderful"; // in .m
Choose the approach that fits best.
选择最适合的方法。
回答by Zumry Mohamed
Depends on your need, for example you have login page. After you logged in you are notifying to other pages in the applications.
取决于您的需要,例如您有登录页面。登录后,您将通知应用程序中的其他页面。
#import <Foundation/Foundation.h>
extern NSString *const DidLoginNotification;
@interface LoginViewController : NSObject
- (void)login;
@end
// LoginViewController.m
#import "LoginViewController.h"
//define extern const in implementation file only once for the whole process
NSString *const DidLoginNotification =
????@"DidLoginNotificationNotified";
@implementation LoginViewController
- (void)login {
????// Perform notification
[[NSNotificationCenter defaultCenter];
sendNotificationName: DidLoginNotification
????????????????????object:nil];
}
The notification receiving party does not need to know the value of the const.
通知接收方不需要知道const的值。

