Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 05-25-2011, 09:25 PM   #1 (permalink)
Registered Member
 
Join Date: Aug 2010
Posts: 14
Jake is on a distinguished road
Default Crash when loading data?

Hello everyone. I'm noticing a very strange problem when trying to access string values from an NSDictionary. I load the data from a URL and parse the JSON with this code, storing only the 'sets' value as an NSArray of NSDictionaries:

Code:
 // Prepare URL request to download data
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]];
    
    // Perform request and get JSON back as a NSData object
    NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    
    // Get JSON as a NSString from NSData response
    NSString *responseData = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
    
    NSDictionary *results = [responseData JSONValue];
    
    resultArray = (NSArray *)[results objectForKey:@"sets"];
I then try to populate a UITableView with that data using the following code (downloadManager was the class where the code above is located):

Code:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [[downloadManager resultArray] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Set up cell
    static NSString *cellIdentifier = @"TableCellIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
    // If a cell doesn't exist, create it.
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    
    // Set up cell
    if ([[downloadManager resultArray] count] > 0) {
        NSDictionary *rowData = [[downloadManager resultArray] objectAtIndex:[indexPath row]]; // <- always produces a crash, without any log info
        [[cell textLabel] setText:[rowData objectForKey:@"title"]];
    }else{
        NSLog(@"No data returned.");
    }
    
    // Return the cell
    return cell;
}
Maybe I'm missing something obvious, but I simply cannot access the data from the dictionary without the simulator crashing. (Even with NSLog.) Is there something wrong with my code that could be causing this?
Jake is offline   Reply With Quote
Old 05-25-2011, 09:51 PM   #2 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
Join Date: Jan 2010
Location: Issaquah, WA
Age: 42
Posts: 1,244
dljeffery is on a distinguished road
Default

Turn on zombies. I would guess that the "[responseData JSONValue]" result is autoreleased (should be) and you don't seem to be doing anything to retain your "resultArray".
__________________
Recall It! Tag your notes. Tag your photos. Tag your thoughts. Tag your life.

Recall It! for iPad

http://www.dljeffery.com
dljeffery is offline   Reply With Quote
Old 05-25-2011, 10:01 PM   #3 (permalink)
Registered Member
 
Join Date: Aug 2010
Posts: 14
Jake is on a distinguished road
Default

Quote:
Originally Posted by dljeffery View Post
Turn on zombies. I would guess that the "[responseData JSONValue]" result is autoreleased (should be) and you don't seem to be doing anything to retain your "resultArray".
I think you may be right. With zombies enabled I'm getting the following error in the console:

*** -[__NSArrayM count]: message sent to deallocated instance 0x5977370

In the DownloadManager.h file, I set the 'resultArray' to 'retain' in the property declaration. Is there something else I need to do to prevent the variable from being released?
Jake is offline   Reply With Quote
Old 05-25-2011, 10:07 PM   #4 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
Join Date: Jan 2010
Location: Issaquah, WA
Age: 42
Posts: 1,244
dljeffery is on a distinguished road
Default

There absolutely is. You need to use your property properly. Right now you're just setting the variable directly, and bypassing the generated setter. There are tons of posts on the forum about this; I'd suggest reading through some of them.

But in short, use "self.resultArray = ...", not just "resultArray = ...".
__________________
Recall It! Tag your notes. Tag your photos. Tag your thoughts. Tag your life.

Recall It! for iPad

http://www.dljeffery.com
dljeffery is offline   Reply With Quote
Old 05-25-2011, 10:15 PM   #5 (permalink)
Registered Member
 
Join Date: Aug 2010
Posts: 14
Jake is on a distinguished road
Default

Quote:
Originally Posted by dljeffery View Post
There absolutely is. You need to use your property properly. Right now you're just setting the variable directly, and bypassing the generated setter. There are tons of posts on the forum about this; I'd suggest reading through some of them.

But in short, use "self.resultArray = ...", not just "resultArray = ...".
I completely forgot about the setters used by Obj-C. (I come from a PHP background.) I changed my code to have the DownloadManager return the array to the view controller instead of accessing it directly, and then properly set it to a variable. The UITableView now displays the data properly, without crashing.

Thank you very much for your help! I'll be sure to remember to use the default setters from now on.
Jake is offline   Reply With Quote
Reply

Bookmarks

Tags
crash, json, nsarray, nsdictionary, uitableview

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 349
6 members and 343 guests
doffing81, dre, iOS.Lover, jenniead38, Kirkout, Wikiboo
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,663
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, LezB44
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 02:11 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0