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有多好?它确实使某些部分的编码变得容易。但它不会破坏物体朝向吗?
5 个解决方案
#1
55
You'll find that extern
is 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.
您将发现,在Cocoa框架中,extern被广泛使用,人们很难找到令人信服的论点,即它们的OO已被“破坏”。相反,可可豆被很好地封装起来,而且只暴露出它必须的东西,通常是通过外部的。全局定义的常量当然是最常见的用法,但不一定是唯一有效的用法。
IMO, using extern
doesn't necessarily "spoil" object orientation. Even in OO, it is frequent to use variables that are accessible from anywhere. Using extern
is the most frequent workaround for the lack of "class variables" (like those declared with static
in 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.
在我看来,使用extern并不一定会破坏物体的朝向。即使在OO中,也经常使用可以从任何地方访问的变量。在Objective-C中,由于缺少“类变量”(比如在Java中声明的静态变量),使用extern是最常见的解决方法。它允许您扩展在编译单元之外引用一个符号的范围,在编译单元中声明一个符号,本质上是承诺它将被某人在某个地方定义。
You can also combine extern
with __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__(可见性(“隐藏”))相结合,以创建一个可以在其编译单元之外使用的符号,但并不在其链接单元之外。我已经将它用于自定义库和框架代码,以恰当地封装高级内部细节。
#2
15
There are some use cases for the extern
keyword in Objective-C.
Aaron Hillegass suggests to create global notification names extern. e.g.:
Objective-C中的extern关键字有一些用例。Aaron Hillegass建议创建外部全局通知名。例如:
extern NSString* const XYYourNotification;
You then define the actual NSString*
in your implementation
然后在实现中定义实际的NSString*
#3
10
It depends on what do you use it for. It is perfectly valid to use it to access to globally defined constants.
If you have a global object, however, I'd suggest using Singleton instead.
这取决于你用它做什么。使用它访问全局定义的常量是完全有效的。但是,如果您有一个全局对象,我建议使用Singleton来代替。
#4
3
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的值。
#5
0
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 extern
in 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):
在.h文件中使用extern,并在.m文件中定义它。这样,将只定义一个引用,每个文件将使用相同的引用(更改反映在所有文件中):
extern NSString *globalVar; // in .h
NSString *globalVar = @"Wonderful"; // in .m
Choose the one that fits best.
选择最适合的。
#1
55
You'll find that extern
is 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.
您将发现,在Cocoa框架中,extern被广泛使用,人们很难找到令人信服的论点,即它们的OO已被“破坏”。相反,可可豆被很好地封装起来,而且只暴露出它必须的东西,通常是通过外部的。全局定义的常量当然是最常见的用法,但不一定是唯一有效的用法。
IMO, using extern
doesn't necessarily "spoil" object orientation. Even in OO, it is frequent to use variables that are accessible from anywhere. Using extern
is the most frequent workaround for the lack of "class variables" (like those declared with static
in 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.
在我看来,使用extern并不一定会破坏物体的朝向。即使在OO中,也经常使用可以从任何地方访问的变量。在Objective-C中,由于缺少“类变量”(比如在Java中声明的静态变量),使用extern是最常见的解决方法。它允许您扩展在编译单元之外引用一个符号的范围,在编译单元中声明一个符号,本质上是承诺它将被某人在某个地方定义。
You can also combine extern
with __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__(可见性(“隐藏”))相结合,以创建一个可以在其编译单元之外使用的符号,但并不在其链接单元之外。我已经将它用于自定义库和框架代码,以恰当地封装高级内部细节。
#2
15
There are some use cases for the extern
keyword in Objective-C.
Aaron Hillegass suggests to create global notification names extern. e.g.:
Objective-C中的extern关键字有一些用例。Aaron Hillegass建议创建外部全局通知名。例如:
extern NSString* const XYYourNotification;
You then define the actual NSString*
in your implementation
然后在实现中定义实际的NSString*
#3
10
It depends on what do you use it for. It is perfectly valid to use it to access to globally defined constants.
If you have a global object, however, I'd suggest using Singleton instead.
这取决于你用它做什么。使用它访问全局定义的常量是完全有效的。但是,如果您有一个全局对象,我建议使用Singleton来代替。
#4
3
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的值。
#5
0
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 extern
in 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):
在.h文件中使用extern,并在.m文件中定义它。这样,将只定义一个引用,每个文件将使用相同的引用(更改反映在所有文件中):
extern NSString *globalVar; // in .h
NSString *globalVar = @"Wonderful"; // in .m
Choose the one that fits best.
选择最适合的。