Spire Spire - 13 days ago 6
iOS Question

JSON and iPhone table view

I'm trying JSON parser for first time and i need a little help
When I try to populate a table view it works OK, but when I scroll the table or select a row the app crashes. I would appreciate any help.

Here are the files I have:

#import <UIKit/UIKit.h>
@class RootViewController;
@interface BooksJsonAppDelegate : NSObject <UIApplicationDelegate> {

UIWindow *window;
UINavigationController *navigationController;
NSMutableArray *statuses;
NSMutableData *responseData;
}
@property(nonatomic, retain)NSMutableArray *statuses;
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UINavigationController *navigationController;

@end


and

#import "BooksJsonAppDelegate.h"
#import "RootViewController.h"
#import "SBJson.h"


@implementation BooksJsonAppDelegate

@synthesize window;
@synthesize navigationController,statuses;


#pragma mark -
#pragma mark Application lifecycle

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

SBJsonParser *parser = [[SBJsonParser alloc] init];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://assignment.golgek.mobi/api/v10/items"]];
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
statuses = [parser objectWithString:json_string error:nil];


self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];

return YES;
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
[responseData setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[responseData appendData:data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog(@"Connection Failed: %@",[error description]);

}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
[connection release];
}

- (void)applicationWillResignActive:(UIApplication *)application {
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
}


- (void)applicationWillTerminate:(UIApplication *)application {
}


#pragma mark -
#pragma mark Memory management

- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
}


- (void)dealloc {
[navigationController release];
[window release];
[super dealloc];
}


@end

then the root view controller

#import <UIKit/UIKit.h>
@class DetailView,BooksJsonAppDelegate;
@interface RootViewController : UITableViewController {
DetailView *detailView;
BooksJsonAppDelegate *booksAppDelegate;
}

@end


and

#import "RootViewController.h"
#import "DetailView.h"
#import "BooksJsonAppDelegate.h"
@implementation RootViewController

#pragma mark -
#pragma mark View lifecycle


- (void)viewDidLoad {
[super viewDidLoad];
booksAppDelegate = (BooksJsonAppDelegate *)[[UIApplication sharedApplication] delegate];

}

#pragma mark -
#pragma mark Table view data source

// Customize the number of sections in the table view.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [booksAppDelegate.statuses count];
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}

// Configure the cell.
NSDictionary *aBook = [booksAppDelegate.statuses objectAtIndex:[indexPath row]];

cell.textLabel.text = [aBook objectForKey:@"title"];
cell.textLabel.adjustsFontSizeToFitWidth = YES;
cell.textLabel.font = [UIFont systemFontOfSize:12];
cell.textLabel.minimumFontSize = 10;
cell.textLabel.numberOfLines = 4;
cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;

return cell;
}

#pragma mark -
#pragma mark Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {


NSDictionary *aBook = [booksAppDelegate.statuses objectAtIndex:[indexPath row]];

detailView = [[DetailView alloc] initWithNibName:@"DetailView" bundle:nil];
// ...
// Pass the selected object to the new view controller.
detailView.title = [aBook objectForKey:@"title"];
[self.navigationController pushViewController:detailView animated:YES];
[detailView release];

}


#pragma mark -
#pragma mark Memory management

- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];

// Relinquish ownership any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
// Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
// For example: self.myOutlet = nil;
}


- (void)dealloc {
[super dealloc];
}


@end

Answer

You will probably need to retain your statuses

statuses = [[parser objectWithString:json_string error:nil] retain];

The JSON parser will return an autoreleased object :)


As Dan points out in the comments the better way of doing this is to set the property like this :

self.statuses = [parser objectWithString:json_string error:nil];

This has the advantage of not leaking memory if you set it twice and you can use KVO ot tell if it's changed. Much better :)

Comments