Help with iphone dev - beyond error
I am just learning iphone development so please forgive me for what is probably a beginner's mistake. I've searched around and didn't find anything specific about my problem, so hopefully this is a simple solution.
The book provides examples of building a table from data hard-coded through an array. Unfortunately it never talks about how to get the data from the url, so I had to see what was going on and this is where my problems come in.
When I debug xcode the count seems to be correct, so I don't understand why it is out of bounds?
This 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 an example of how the book works, 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
a source to share
You are over-executing listItems as well as ans, this is probably your problem ... If you don't do selection or save (usually) the returned objects are automatically saved, so you don't need to release them. [NSString ...] returns an auto-emitted string and also calls [ans componentsSeparatedByString: @ ","] ... hope that helps
a source to share