views:

89

answers:

1

I'm just learning iphone development, so please forgive me for what is probably a beginner error. I've searched around and haven't found anything specific to my problem, so hopefully it is an easy fix.

The book has examples of building a table from data hard coded via an array. Unfortunately it never really tells you how to get data from a URL, so I've had to look that up and that is where my problems show up.

When I debug in xcode, it seems like the count is correct, so I don't understand why it is going out of bounds?

This is the error message:

2010-05-03 12:50:42.705 Simple Table[3310:20b] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSCFArray objectAtIndex:]: index (1) beyond bounds (0)'

My URL returns the following string:

first,second,third,fourth

And here is the iphone code with the book's working example commented out

#import "Simple_TableViewController.h"

@implementation Simple_TableViewController
@synthesize listData;

- (void)viewDidLoad
{
    /*
     //working code from book
     NSArray *array = [[NSArray alloc] initWithObjects:@"Sleepy", @"Sneezy", @"Bashful", @"Bashful", @"Happy",
                      @"Doc", @"Grumpy", @"Thorin", @"Dorin", @"Norin", @"Ori", @"Balin", @"Dwalin", @"Fili", @"Kili", @"Oin", 
                      @"Gloin", @"Bifur", @"Bofur", @"Bombur", nil];
     self.listData = array;
     [array release];
     */

     //code from interwebz that crashes 
     NSString *urlstr = [[NSString alloc] initWithFormat:@"http://www.mysite.com/folder/iphone-test.php"];
     NSURL *url = [[NSURL alloc] initWithString:urlstr];
     NSString *ans = [NSString stringWithContentsOfURL:url];
     NSArray *listItems = [ans componentsSeparatedByString:@","];

     self.listData = listItems;

     [urlstr release];
     [url release];
     [ans release];
     [listItems release];


    [super viewDidLoad];
}

- (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.
}

- (void)viewDidUnload 
{
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    self.listData = nil;
    [super viewDidUnload];
}


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

#pragma mark -
#pragma mark Table View Data Source Methods

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.listData count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SimpleTableIdentifier];

    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc]
                 initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SimpleTableIdentifier] autorelease];
    }

    NSUInteger row = [indexPath row];
    cell.textLabel.text = [listData objectAtIndex:row];
    return cell;
}

@end
+2  A: 

YOu are overreleasing listItems as well as ans, that is probably your problem...WHen you dont do an alloc or retain (usually) the objects returned are autoreleased, therefore you dont need to release them. [NSString ...] returns an auto released string and so does the call [ans componentsSeparatedByString:@","]...hope that helps

Daniel
Awesome! Thanks Daniel and all the rest of you! That fixed the problem. I wasn't aware of the auto releasing. Either I missed that in the book, or it isn't in there. Thanks again for the fix and the explanation! I will make sure to vote up as soon as I am allowed. Have to get to 15 I guess.
dusk
*You* own objects *you* create (create with a "new", "alloc" or "copy" function), you don't own objects created by other functions so you shouldn't release them. http://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmObjectOwnership.html#//apple_ref/doc/uid/20000043-SW1 and http://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmObjectOwnership.html#//apple_ref/doc/uid/20000043-SW3
progrmr
also, when you create function in your classes that return objects, you should make sure they return autoreleased objects or objects with retain count of 0, with that convention the caller never has to worry that a call to a function will return an object with retain count of +1.
Daniel