How to use extern keyword to define global ivars, does anybody know this question?Thanks in advance.
Update from comments:
OK, I searched from the Internet that I should declare the variable in .h file,like below shows,
int name;
@interface testGlobal : NSObject
@end
And when I want to use it in one .m file, I should write like below,
extern int name;
@implementation ViewController
When I use it in the implementation file,I got the following error
duplicate symbol _name in: /Users/jay_Gao/Library/Developer/Xcode/DerivedData/Audition-bahpwhcmpnzxppajcliyuhtuaywz/Build/Intermediates/Audition.build/Debug-iphonesimulator/Audition.build/Objects-normal/x86_64/Test.o /Users/jay_Gao/Library/Developer/Xcode/DerivedData/Audition-bahpwhcmpnzxppajcliyuhtuaywz/Build/Intermediates/Audition.build/Debug-iphonesimulator/Audition.build/Objects-normal/x86_64/testGlobal.o ld: 1 duplicate symbol for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)', can you explain this please? – I build successfully by remove the
#import "testGlobal.h"
from test.m. But what if I want to import that file testGlobal.h
in other files, can not I do it? Should I only declare the global variable in AppDelegate.h
? Since on other file will import AppDelegate.h
?
I test it from my side and I can build it successfully, but I feel confuse that why should the global variable be declared in that way. The variable is outside any code block.