开发者

Iphone: threading issue?

I am initialing a dictionary in viewDidLoad and using it to create table view cells.

The first load works fine. But as soon as I scroll the tabl开发者_StackOverflowe view to see the item (not displayed at the bottom) the app crashes. Through the Debugger I noticed the address of the dictionary item "rootItemsDict" changes when I did the scroll. Not able to figure out why that is. From my understanding the address of an object initialized once should remain same, at least within the given class instance. Any thoughts?

    - (void)viewDidLoad {
        [super viewDidLoad];

            NSString *path = [[NSBundle mainBundle] pathForResource:@"menu" ofType:@"plist"];
            rootItemsDict = [NSDictionary dictionaryWithContentsOfFile:path];
   } 



    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    NSString *title = (NSString *)[[[rootItemsDict objectForKey:@"rootMenuItems"] objectAtIndex:row] objectForKey:@"heading"];


+dictionaryWithContentsOfFile: returns an autoreleased instance. To take ownership you need to explicitly retain it:

rootItemsDict = [[NSDictionary dictionaryWithContentsOfFile:path] retain];

... or use the alloc/init form:

rootItemsDict = [[NSDictionary alloc] initWithContentsOfFile:path];

... or if you have a suitable property declaration (retain) use the setter:

self.rootItemsDict = [NSDictionary dictionaryWithContentsOfFile:path];

I recommend to read the Memory Management Programming Guide, especially the section on object ownership.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜