开发者

TableViewController crashes when calling a retained property?

I have a Table View Controller and during its initialisation I set an NSArray property which is then used in the cellForRow开发者_高级运维AtIndexPath method to display the data on the table.

But, when I touch a row, once I call this retained NSArray property it says EXC_BAD_ACCESS!

FYI the property is defined as shown below, and uses a custom getter function:

@property (nonatomic,retain) NSArray *dataList;

and in the .m file:

@synthesize dataList;

- (NSArray *)dataList
{
    if (!dataList)
    {
        NSString *p = [kind lowercaseString];
        NSString *s = [[NSBundle mainBundle] pathForResource:p ofType:@"txt"];
        NSLog(@"%@",s);

        NSData *dataRep = [NSData dataWithContentsOfFile:s];
        NSPropertyListFormat format;
        dataList = [NSPropertyListSerialization propertyListFromData: dataRep
                                                     mutabilityOption: NSPropertyListImmutable
                                                               format: &format
                                                     errorDescription: nil];
        if (dataList.count == 0)
            NSLog(@"Fetch failed!");
    }

    return dataList;
}

Any suggestions?


This is the problem:

dataList = [NSPropertyListSerialization propertyListFromData ...

This function does not begin with alloc, copy, or retain, therefore it returns an autoreleased object. However, you need it to be retained so that it stays around.

You have two options:

self.dataList = [NSPropertyListSerialization propertyListFromData ...

or,

dataList = [[NSPropertyListSerialization propertyListFromData ...] retain];
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜