views:

369

answers:

3

What did I do wrong? I just modified the Navigation Based application code a bit to read and display a JSON string. It crashes when I scroll up the list with the message Objc_msgSend and points at this as the problem: cell.textLabel.text=[[locations objectAtIndex: storyIndex] objectForKey: @"title"];

 #import "RootViewController.h"
 #import "JSON.h"


 @implementation RootViewController

 @synthesize locations;

 - (NSString *)stringWithUrl:(NSURL *)url
 {
    // Construct a String around the Data from the response
    return [[NSString alloc] initWithContentsOfURL:url];
 }

 -(void)jsonLoad {

    NSURL *URL = [NSURL URLWithString:@"http://bombaytokyo.com/whrru/jsonexample.html"];

     NSString *jsonstring = [self stringWithUrl:URL];
    NSLog(jsonstring);
    locations = [jsonstring JSONValue];
    [jsonstring release];
 }


 - (void)viewDidLoad {
     [super viewDidLoad];

     // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
     // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    [self jsonLoad];

 }


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

    // Release any cached data, images, etc that aren't in use.
    [locations release];
 }

 - (void)viewDidUnload {
    // Release anything that can be recreated in viewDidLoad or on demand.
    // e.g. self.myOutlet = nil;
 }


 #pragma mark Table view methods

 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
     return 1;
 }


 // Customize the number of rows in the table view.
 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
 //    return 0;
    return [locations 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.
    int storyIndex = [indexPath indexAtPosition: [indexPath length] - 1];
    NSLog(@"Index Path: %i",indexPath);
    //NSLog(title);
    cell.textLabel.text=[[locations objectAtIndex: storyIndex] objectForKey: @"title"];

     return cell;
 }

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


 @end
A: 

0 based arrays, id length is 0 the 0 - 1 = -1 this will obviously cause an exception.

[indexPath indexAtPosition: [indexPath length] - 1]

change to

[indexPath indexAtPosition: [indexPath length]]

and are you sure you dont want indexPath.row. it looks like you are just returning the length each time.

Bluephlame
A: 

Still doesn't work.

I changed it to:

NSMutableString *title = [[locations objectAtIndex:indexPath.row] objectForKey: @"title"];

NSLog gives:

kill
quit

The Debugger has exited with status 0.
[Session started at 2009-05-31 13:12:22 +0900.]
2009-05-31 13:12:25.078 WHRRU[2066:20b] [
{
"title" :   "Apple Retail Store - SoHo",
"location" :    "103 Prince Street New York City"
},
{
"title" :   "New York University - Stern School of Business",
"location" :    "44 W 4th St, New York, NY"
},
{
"title" :   "Massachusetts Institute of Technology‚Äé",
"location" :    "77 Massachusetts Ave, Cambridge, MA"
}
]
2009-05-31 13:12:25.082 WHRRU[2066:20b] Index Path: 0
2009-05-31 13:12:25.083 WHRRU[2066:20b] Apple Retail Store - SoHo
2009-05-31 13:12:25.089 WHRRU[2066:20b] Index Path: 1
2009-05-31 13:12:25.091 WHRRU[2066:20b] New York University - Stern School of Business
2009-05-31 13:12:25.092 WHRRU[2066:20b] Index Path: 2
2009-05-31 13:12:25.095 WHRRU[2066:20b] Massachusetts Institute of Technology‚Äé

[Session started at 2009-05-31 13:12:28 +0900.]
2009-05-31 13:12:28.493 WHRRU[2066:20b] Index Path: 0
GNU gdb 6.3.50-20050815 (Apple version gdb-966) (Tue Mar 10 02:43:13 UTC 2009)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "i386-apple-darwin".sharedlibrary apply-load-rules all
Attaching to process 2066.
(gdb)
is return [locations count]; returning 3?Also there dosn't seem to be a crash in that log. It could also be aretain issue on your strings. are you releasing them anywhere? if so uncomment that and try (it may give you a memmory leak but it will tell you if it is working.)
Bluephlame
A: 

solution:

self.locations = [jsonstring JSONValue];

Thanks for all the suggestions.