我刚发现我无法从其他类释放我在 appDelegate 中设置的变量。
例子:
我有一个类 myClass
。
In myClass.m if I do :
appDelegate = [[UIApplication sharedApplication] delegate];
appDelegate.aString = [[NSString alloc] init]; //aString is declared and synthesized in appDelegate class.
[appDelegate.aString stringWithString:@"test"];
[appDelegate.aString release];
NSLog(appDelegate.aString);
我在控制台中得到“测试”。
我做错了什么? :/
最佳答案
您没有正确使用属性。您的代码应为:
appDelegate = [[UIApplication sharedApplication] delegate];
appDelegate.aString = [NSString stringWithString:@"test"]; // This will be retained by your appDelegate
appDelegate.aString = nil; // This will cause your appDelegate to release the variable
NSLog(@"%@",appDelegate.aString); // don't put objects directly in to NSLog
就目前而言,您已经创建了一个自动释放的对象(在 stringWithString 中),然后通过将其设置为您的属性来再次保留它。
关于iphone - 无法释放我的 appDelegate 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7256103/