0

I'm a little stumped, for some reason the following code causes my app to crash on a real iPhone although it runs fine in the simulator, all it does it grabs some json and puts it in a list view, does anyone have any idea why it keeps crashing? Any help is greatly appreciated!

--------SecondViewController.m------

#import "SecondViewController.h" @interface SecondViewController () @end @implementation SecondViewController - (void)viewDidLoad { [super viewDidLoad]; [self fetchPrices]; } - (void)viewDidUnload { [super viewDidUnload]; } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); } - (void)fetchPrices { dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ NSData* data = [NSData dataWithContentsOfURL: [NSURL URLWithString: @"http://url.php"]]; NSError* error; prices = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error]; dispatch_async(dispatch_get_main_queue(), ^{ [self.tableView reloadData]; }); }); } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return prices.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"PriceCell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } NSDictionary *price = [prices objectAtIndex:indexPath.row]; NSString *text = [price objectForKey:@"name"]; NSString *name = [price objectForKey:@"price"]; cell.textLabel.text = text; cell.detailTextLabel.text = [NSString stringWithFormat:@"Price: %@", name]; return cell; } @end 

-----SecondViewController.h----

#import <UIKit/UIKit.h> @interface SecondViewController : UITableViewController { NSArray *prices; } - (void)fetchPrices; @end 

---- Crash Log ---- http://pastebin.com/cnf6L7Jf

3
  • 5
    What exception are you getting? Which line does it crash on? Commented Oct 9, 2012 at 15:04
  • @PhillipMills I added the crash log to pastbin! Commented Oct 9, 2012 at 15:43
  • Can you set an exception breakpoint? Commented Oct 9, 2012 at 15:51

1 Answer 1

1

Problem is that NSArray *prices is at random value between the time your fetching the price and when you're dealing with the value. Secondly you're not retaining it. So prices can be garbage value too.

The cleaner way is to

@property(nonatomic, retain)NSArray *prices; /**/ @synthetise prices; // then SELF.prices = [[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error]; 

so that "prices" is nil when you're initialing your tablecontroller, and always available when u need it.

Don't forget to release it in your dealloc method

Sign up to request clarification or add additional context in comments.

5 Comments

All instance variables are initialized to nil. Although using a property is good practice here.
Yeah you're actually right. Every global class ivar are nil/0 when initialized. By the way if you need prices to be reAllocated lot of time, keep synthesized method. If you only need once, you can forget @synthetise but you need to retain explicitly the JSON data
I'm a little confused, am I suppose to put that in my .h file?
Yeah, or you can insert it into ur category
It's saying SELF is an unknown type.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.