核心数据布尔属性比较

Cod*_*ver 2 iphone xcode core-data objective-c ios

我有一个tableViewController,并在:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
Run Code Online (Sandbox Code Playgroud)

我有这个代码:

    MinorGoal *minor = [self.array objectAtIndex:indexPath.row];
if (minor.finishedBoolean == [NSNumber numberWithBool:YES]){
    // code to make check mark
    UITableViewCell *indexPathCell = [tableView cellForRowAtIndexPath:indexPath];
    indexPathCell.accessoryType = UITableViewCellAccessoryCheckmark;
    NSLog(@"the %@, is finished", minor.title);
}
cell.textLabel.text = minor.title;
Run Code Online (Sandbox Code Playgroud)

所有我想要的是检查'finishedBoolean'arrtibute为'minor',如果它是YES,那么它所在的单元格必须有一个复选标记,当我运行上面的代码时,我得到NSLog:

        NSLog(@"the %@, is finished", minor.title);
Run Code Online (Sandbox Code Playgroud)

这意味着if语句正在工作,但为什么单元附件没有设置为checkMark?这是这一行:

        indexPathCell.accessoryType = UITableViewCellAccessoryCheckmark;
Run Code Online (Sandbox Code Playgroud)

谢谢,抱歉新手

Fog*_*ter 8

我猜CoreData ManagedObject是一个minor会成为的对象.finishedBooleanNSNumber

您的检查失败,因为您正在检查两个对象是同一个对象.==不检查对象的相等值.

如果声明,有几种方法可以做到这一点.

if ([minor.finishedBoolean boolValue])

if ([minor.finishedBoolean isEqualToNumber:@YES])

if ([minor.finishedBoolean boolValue] == YES)
Run Code Online (Sandbox Code Playgroud)

在所有这些中,我会选择第一个.

希望这可以帮助.