-1

I know that 'extern' means that it's declaration and definition are not available in the same source files. However i don't see any significant use of it.

Lets assume i have three classes Employee, Department and Library.

lets say we have something like this in Library class

extern NSString *abc;

we use it something like this in Department class.

Department.h

NSString *abc;

Department.m

abc = @"abcdefg";

and something like this is Employee class.

Employee.m

NSString *abc = @"xyz";

When we look at classes Employee and Department they look just like normal variable declarations and definitions.

So, My question is what is it's special power?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Prateek Raj
  • 3,966
  • 6
  • 39
  • 50
  • http://nshipster.com/c-storage-classes/ – Christian Fox Sep 30 '14 at 20:56
  • extern is a C feature. Objective-C have it only because ObjC is trying to be C-compatible. So yes, in purely object-oriented ObjC code you will not need it. – Fermat's Little Student Sep 30 '14 at 21:10
  • @Will More correctly, Objective-C is a full superset of C. It's not trying to be C-compatible, it *is* C, plus a lot more. But you are right, a properly designed, truly OO Objective-C code base isn't going to use some of the C concepts like `extern` global variables. – rmaddy Sep 30 '14 at 22:13

1 Answers1

1

In this case, extern tells the compiler not to complain that there's a declaration of the variable without a definition of it. Though, Xcode doesn't complain about this anyway because all Objective-C global variables are secretly declared extern.

You could technically leave the extern directive off, but you might as well be explicit. It still means something to the programmer who sees it.

Marcus Adams
  • 53,009
  • 9
  • 91
  • 143