获取错误:*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSNull isEqualToString:]: unrecognized selector sent to instance'
...
SIGABRT
来自else cell.textLabel.text = @"Blank";
行:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {UITableViewCell *cell = nil;
static NSString *CellIdentifier = @"Cell";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
NSManagedObjectContext *context = [self managedObjectContext];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Pattern" inManagedObjectContext:context];
// Edit the sort method.
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"patternName" ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)];
NSArray *sortDescriptors = [[[NSArray alloc] initWithObjects:sortDescriptor, nil] autorelease];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setSortDescriptors:sortDescriptors];
[fetchRequest setEntity:entity];
_patterns = [context executeFetchRequest:fetchRequest error:nil];
[fetchRequest release];
NSArray *names = [_patterns valueForKey:@"patternName"];
NSArray *urls = [_patterns valueForKey:@"patternUrl"];
if (names!= nil) {
cell.textLabel.text = [names objectAtIndex:indexPath.row];
}
else cell.textLabel.text = @"Blank";
cell.detailTextLabel.text = [urls objectAtIndex:indexPath.row];
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
return cell;
}
我可以添加已定义的记录(复制用户正在查看的页面的URL,将URL插入到url字段和name属性中)。编辑或创建新记录会弹出ModalViewController(显示现有记录)并捕获文本字段中的数据(用于新记录)。选择保存崩溃,只更改最后添加的已定义记录的记录,即使它已被编辑,并将其作为新记录添加,保留旧记录。我认为我跟踪细胞数量有些不对劲。
任何建议都会受到最高的赞赏。
答案 0 :(得分:1)
您从executeFetchRequest获得的是对象数组而不是属性。您需要遍历对象数组并从每个对象中获取属性
此
_patterns = [context executeFetchRequest:fetchRequest error:nil];
[fetchRequest release];
NSArray *names = [_patterns valueForKey:@"patternName"];
NSArray *urls = [_patterns valueForKey:@"patternUrl"];
if (names!= nil) {
cell.textLabel.text = [names objectAtIndex:indexPath.row];
}
应该成为
_patterns = [context executeFetchRequest:fetchRequest error:nil];
[fetchRequest release];
if (names!= nil) {
cell.textLabel.text = [[_patterns objectAtIndex:indexPath.row] valueForKey:@"patternName"];
}
答案 1 :(得分:0)
我猜这个问题实际上是在前一行:
cell.textLabel.text = [names objectAtIndex:indexPath.row];
我认为[name objectAtIndex:indexPath.row]返回的对象是NSNull。
尝试以下方法:
NSString *name = nil;
if (names) {
name = [names objectAtIndex:indexPath.row];
if (name && name == (id)[NSNull null]) {
name = nil;
}
}
if (!name) {
name = @"Blank";
}
cell.textLabel.text = name;
编辑:在最后的if语句中修正了我的逻辑。