2012-01-17 54 views
1

請參閱下面的代碼。爲什麼訪問[self.objects計數]拋出這個錯誤,當直接在它之前的行證明self.objects存在?ios 5 - EXC_BAD_ACCESS錯誤

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    // Return the number of rows in the section. 
    NSLog(@"HERE: %@", self.objects); //this logs the array - no error 
    NSLog(@"num rows: %@", [self.objects count]); //this line throws the error 
    return [self.objects count]; 
} 
在.h文件

我有這樣的:

@interface YouTubeViewController_iPad : UITableViewController 
{ 
    NSArray *_objects; 
} 

@property (nonatomic, retain) NSArray *objects; 

,並在.m文件:

@synthesize objects = _objects; 

回答

2

您需要正確格式的日誌字符串:

NSLog(@"num rows: %@", [self.objects count]); //this line throws the error 

[self.objects count]返回一個NSInteger,它是一個整數。理解一個整數不是一個對象是很重要的。

試試這個:

NSLog(@"num rows: %i", [self.objects count]); //Notice the string formatter 
2

錯誤的:

NSLog(@"num rows: %@", [self.objects count]); //this line throws the error 

的更新:

NSLog(@"num rows: %d", [self.objects count]); //this line throws the error