开发者

Accessing to a NSMutableArray in UITableViewController crashes

I'm new in Object-c and want to make a UITableViewController based app with JSON data source in Xcode 4. I imported the JSON framework and defined an NSMutableArray to fill it with the r开发者_高级运维esponse:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {

[connection release];

NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
[responseData release];

items = [responseString JSONValue];

[self.tableView reloadData];
}

I Everything went well but when I try to access my items array in the

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

function, it crashes my app.

What could be the problem? Thanks in advance!

UPDATE: I modified the array filling part of the code and it solved the crash problem: NSMutableArray *a = [responseString JSONValue];

for(NSDictionary *it in a) {

    [items addObject:it];
}

But I still don't know why...


It semms like you assign the JSON-Value to an instance variable. The object is autoreleased ("JSONValue" doesn't contain the words alloc, init or copy) so it will go away some near time in the future.

Try adding a property for your object: Header:

@property (nonatomic, retain) NSArray *items;

Implementation:

@synthesize items;

...

self.items = [responseString JSONValue];

...

- (void)dealloc {
    ...
    self.items = nil;
    [super dealloc];
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜