ios 5 - EXC_BAD_ACCESS错误

时间:2012-01-17 05:44:33

标签: ios arrays memory-management

请参阅下面的代码。为什么访问[self.objects count]会抛出此错误,直到它之前的行证明self.objects存在?

- (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 个答案:

答案 0 :(得分: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

答案 1 :(得分:2)

错误:

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

更新:

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