iphone - 无法释放我的 appDelegate 变量

标签 iphone objective-c ios cocoa-touch

我刚发现我无法从其他类释放我在 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/

相关文章:

ios - 如何使用 Swift 打印数据类型变量的内容?

ios - 从 Assets Library URL iOS 获取 MIME 类型

ios - 在应用程序内更改语言后更新所有 UIViewController

iphone - CABasicAnimation 对drawInContext 的调用导致实例变量重置为零

objective-c - 我的 UINavBar 不响应触摸,并随 tableview 滚动

ios - 我们如何在选择段后调用 PickView 的 didSelectRow?

ios - 如何找到 UITextView 中的第一个 URL?

iphone - 从 iPhone 应用程序远程访问 SQL 数据库

iphone - 如何在主视图计算的反面 View 上获取分段控件的值?

iphone - CorePlot - 设置 y 轴范围以包括所有数据点