我正在使用[[UIApplication sharedApplication] delegate]来在多个类之间共享一个变量。我在AppDelegate中设置了值。我能够从myAppDelegate.m中NSLog它并查看该值。然后当我的Tabs之一加载时,我尝试NSLog该值,但它会导致崩溃:
myAppDelegate *app = (myAppDelegate *)[[UIApplication sharedApplication] delegate];
NSLog(@"Value:%@ ", app.delegateVar); // <--- Causes Crash
基本上看起来是创建了一个新的app.delegateVar实例?delegateVar在myAppDelegate.h中定义,然后在myAppDelegate.m中执行以下操作:
- (void)applicationDidFinishLaunching:(UIApplication *)application {
...
[delegateVar release];
delegateVar = [NSString stringWithFormat:@"Test Value"];
NSLog(@"%@",delegateVar);
...
}
delegateVar
属性是什么,它是如何定义的? - Shaggy FrogAppDelegate.m
文件中的类命名为AppDelegate
,而不是myAppDelegate
。按照惯例,类名应该大写,所以"myAppDelegate *app = ...
"看起来很可疑。 - David GelharNSMutableArray
吗?在你的applicationDidFinishLaunching
中它被设置为了一个字符串。 - Sam Dolan