views:

96

answers:

3

Hi, I'am setting the text of labels with the content of NSMutableString_s which are objects of a NSMutableDictionray ... the problem that all things work fine when i load the page for two times (so i push ,after i pop...) and with the third push the program can't read the content of one of the NSMutableString_s of the NSMutableDictionary..so when it turns to the step of setting the value of the UILabel it didn't find the value...

there is the code where the exception appear:

- (void)viewDidLoad{
    [super viewDidLoad];
    NSMutableDictionary *item=[days objectAtIndex:0];
    NSString *title1=[item objectForKey:@"week_day"];
    name1.text=title1;
    [title1 release];
+1  A: 

You should not release title1 - you don't own the string returned by -objectForKey: and didn't take ownership by retaining it.
I suggest to read through the Cocoa Memory Management Guide to prevent that in the future.

Assuming that text is a retain property: with mutable strings, you should assign copies of the string to avoid them being changed under you:

NSString *title1 = [[item objectForKey:@"week_day"] copy];
name1.text = title1;
[title1 release]; // copy means taking ownership, so release

The following is a simplified example of what could be happening with your code as posted:

// 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 :(
Georg Fritzsche
there is a link to an image which descibes the bug (when i am debugging) and you can remark that the program didn't assign a value to "title1" http://www.4shared.com/photo/ErtRxLwr/Picture_1.htmland i remind that all things worked fine when i pushed the view two times ago
Dingua
A: 

Thank you all, now it works fine when i escape the the transition with intermediate variables ,but what i didn't understood why it works for the two "pushing" actions before!!!

name1.text=[[days objectAtIndex:0] objectForKey:@"week_day"];
Dingua
A: 

But with the answer of Georg Fritzsche i understand it now so THANK YOU Georg Fritzsche!!!!

Dingua
Good to hear, but please don't add answers instead of comments - you can comment on all answers to your qestions. Stack Overflow is a Qestion/Answer site, not a forum.
Georg Fritzsche
oki ...i am new you see..
Dingua
Sure, i am just pointing it out :) Its a bit hard to follow if answer are responses etc.
Georg Fritzsche