在目标C中使用extern
在目标C中使用extern有多好? 它确实使编码的一些部分容易..但不破坏对象的方向?
你会发现extern在Cocoa框架中被广泛使用,并且很难find一个令人信服的论证,即它的OO被“宠坏了”。 相反,cocoa被封装得很好,通常只能通过外部的方式暴露出来。 全局定义的常量当然是最常用的用法,但不一定是唯一有效的用法。 
 国际海事组织,使用extern并不一定“破坏”面向对象。 即使在OO中,也可以使用从任何地方访问的variables。 使用extern是Objective-C中缺less“类variables”(如用Java中的static声明)的最常见的解决方法。 它允许您扩展可以引用符号的范围,而不是在声明的编译单元之外,实质上是承诺它将由某人在某处定义。 
 你也可以把extern和__attribute__((visibility("hidden")))起来创build一个符号,可以在编译单元外部使用,但不能在其链接单元之外使用。 我已经使用这个自定义库和框架代码来正确封装更高层次的内部细节。 
  Objective-C中的extern关键字有一些用例。 
 亚伦Hillegassbuild议创build全球通知名称外部。 例如: 
 extern NSString* const XYYourNotification; 
 然后在你的实现中定义实际的NSString* 
 这取决于你用什么。 使用它来访问全局定义的常量是完全有效的。 
 如果你有一个全局对象,我会build议使用Singleton 。 
取决于你的需求,例如你有login页面。 login后,您将通知应用程序中的其他页面。
 #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]; } 
通知接收方不需要知道const的值。