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?
5 Answers
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.
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.
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.

- 44,553
- 16
- 113
- 131
-
Hm.. But it does decrease Code readability if the use is not documented to a larger extent. – Gaurav Ghate Jul 25 '10 at 07:08
-
Is there a way to use this with a class declaration? (i.e. http://stackoverflow.com/questions/22798660/extern-with-class-in-objective-c) – Senseful Apr 01 '14 at 23:12
-
Also consider FOUNDATION_EXPORT as it works with C++ code as well. http://stackoverflow.com/a/10953284/142358 – Steve Moser Nov 03 '15 at 15:23
There are some use cases for the extern
keyword in Objective-C.
Aaron Hillegass suggests to create global notification names extern.
e.g.:
extern NSString* const XYYourNotification;
You then define the actual NSString*
in your implementation

- 34,177
- 3
- 81
- 112
-
Where can I find more about Hillegass' suggestion? Do you know the URL? – Joshua May 24 '11 at 19:51
-
1I first saw it in "Cocoa Programming for Mac OS X" (Aaron Hillegass ). One of the best Cocoa related books I read so far. http://www.bignerdranch.com/book/cocoa®_programming_for_mac®_os_x_3rd_edition – Thomas Zoechling May 24 '11 at 21:15
-
Why do I need to use extern ? Can't I just declare a static variable ? Thanks – aneuryzm Sep 07 '11 at 08:24
-
Static variables are not available in objective C, hence the use of extern variables. – d00dle May 28 '13 at 15:44
-
4If you're stating that "static variables are not available in Objective-C", then what does this mean for the static keyword that I use all the time? – Alex Zavatone Jul 12 '13 at 16:20
-
When you declare something using static you are not writing Objective-C you are writing C++. – Matthew Cawley Jan 19 '16 at 15:18
Another example of a problem when not using 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:
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 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):
extern NSString *globalVar; // in .h
NSString *globalVar = @"Wonderful"; // in .m
Choose the approach that fits best.

- 1,367
- 15
- 23
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.

- 9,318
- 5
- 46
- 51