2011-11-13 15 views

回答

1

是的,這是完全可能的。

只要記住要在翻譯單位中使用extern關鍵字,您將使用全局變量。

例如,在你的main.m文件:

NSString* yourGlobalStr = [[NSString alloc] initWithString:@"Global!"]; 

而且,你要使用它,在other_file.m地說:

extern NSString* yourGlobalStr; 

- (void) m 
{ 
    NSLog(yourGlobalStr); 
} 
0

你可以聲明它在你的應用程序委託,然後從任何地方訪問它:

MyAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate]; 
NSLog(appDelegate.myString); 
相關問題