请参见下面的代码。当直接在它前面的行证明self.objects存在时,为什么访问[self.objects count]会引发此错误?

- (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;

最佳答案

您需要正确设置日志字符串的格式:

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

09-10 08:28
查看更多