2010-05-03 24 views
2

我剛剛學習iPhone的發展,所以請原諒我什麼可能是一個初學者的錯誤。我周圍搜索,沒有找到任何具體的問題,所以希望這是一個簡單的修復。幫助與iPhone開發 - 超越界限錯誤

這本書的例子是通過一個數組硬編碼來建立表格。不幸的是,它從來沒有真正告訴你如何從URL中獲取數據,所以我不得不查看那個,這就是我的問題出現的地方。

當我在xcode中調試時,似乎count是正確的,所以我不明白爲什麼它出界了?

這是錯誤消息:

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)' 

我的URL返回以下字符串:

first,second,third,fourth 

,這裏是用書的工作示例的iphone代碼註釋掉

#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
+0

我沒有看到問題。你在incellForRowAtIndexPath的return語句處停止調試器,並在那裏檢查listData(和row)嗎?確保listData非零,並且仍然有數據。 – progrmr 2010-05-04 03:03:17

+2

listItems在這裏嘗試發佈發佈聲明,看看會發生什麼 – Daniel 2010-05-04 03:34:14

+0

@Daniel:aha,listItems是autoreleased,不需要發佈它。你應該把它作爲答案。 – progrmr 2010-05-04 04:41:01

回答

2

YOu是過度發佈listItems以及答案,這可能是你的問題......當你不做一個人loc或保留(通常)返回的對象是autoreleased,因此你不需要釋放它們。 [NSString ...]返回一個自動釋放的字符串,這樣的調用[ans componentsSeparatedByString:@「,」] ...希望可以幫助

+0

太棒了!謝謝丹尼爾和你們所有人!這解決了問題。我不知道自動釋放。要麼我錯過了那本書,要麼它不在那裏。再次感謝修復和解釋!我一定會盡快投票。我猜想必須達到15。 – dusk 2010-05-04 16:22:09

+1

*您*自己創建的對象*(使用「新建」,「分配」或「複製」功能創建),您不擁有其他功能創建的對象,因此您不應釋放它們。 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 2010-05-04 16:55:51

+0

此外,當您在返回對象的類中創建函數時,應確保它們返回自動釋放對象或保留計數爲0的對象,使用該約定,調用者永遠不必擔心對函數的調用將返回保留計數爲+1的對象。 – Daniel 2010-05-04 17:15:00