我正在设置带有NSMutableString_s内容的标签文本,它们是NSMutableDictionray的对象......当我加载页面两次时所有工作正常的问题(所以我推,我弹出后... 。)并且在第三次推送时程序无法读取NSMutableDictionary..so的NSMutableString_s之一的内容,当它转到设置UILabel的值的步骤时,它没有找到值... < / p>
出现异常的代码:
- (void)viewDidLoad{
[super viewDidLoad];
NSMutableDictionary *item=[days objectAtIndex:0];
NSString *title1=[item objectForKey:@"week_day"];
name1.text=title1;
[title1 release];
答案 0 :(得分:1)
您不应该发布title1
- 您不拥有-objectForKey:
返回的字符串,并且没有保留所有权。
我建议您仔细阅读Cocoa Memory Management Guide以防止将来发生这种情况。
假设text
是retain
属性:使用可变字符串,您应该分配字符串的副本以避免在您下面更改它们:
NSString *title1 = [[item objectForKey:@"week_day"] copy];
name1.text = title1;
[title1 release]; // copy means taking ownership, so release
以下是您的代码发布时可能发生的事情的简化示例:
// entering -viewDidLoad the first time:
NSString *title1=[item objectForKey:@"week_day"];
// lets assume that the strings retain count is 1 here
name1.text=title1;
// setter retains, retain count now 2
[title1 release];
// retain count now 1
// entering -viewDidLoad the second time:
NSString *title1=[item objectForKey:@"week_day"];
// assuming nothing else did retain it, strings retain count is still 1
name1.text=title1;
// you assigned the same object, retain count still 1
[title1 release];
// strings retain count now 0 - will be deallocated :(
答案 1 :(得分:0)
谢谢大家, 现在,当我用中间变量逃避转换时,它工作正常,但我不明白为什么它适用于之前的两个“推动”动作!!!
name1.text=[[days objectAtIndex:0] objectForKey:@"week_day"];