Core Data 布尔属性比较

Posted

技术标签:

【中文标题】Core Data 布尔属性比较【英文标题】:Core Data Boolean attribute compare 【发布时间】:2013-09-19 16:09:52 【问题描述】:

我有一个 tableViewController,在:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

我有这个代码:

    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;

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

        NSLog(@"the %@, is finished", minor.title);

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

        indexPathCell.accessoryType = UITableViewCellAccessoryCheckmark;

谢谢,对新手感到抱歉

【问题讨论】:

【参考方案1】:

我猜 CoreData ManagedObject 是 minor 对象,它将使 finishedBoolean 成为 NSNumber

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

有几种方法可以做到这一点。

if ([minor.finishedBoolean boolValue])

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

if ([minor.finishedBoolean boolValue] == YES)

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

希望这会有所帮助。

【讨论】:

是的,这行得通,它知道 boolValue 设置为'YES',但单元格附件类型没有更改为复选标记,这是问题所在。 哦,对不起。只需使用cell.accessoryType = blah,删除“indexPathCell”代码。

以上是关于Core Data 布尔属性比较的主要内容,如果未能解决你的问题,请参考以下文章

为啥我存储在 Core Data 中的布尔值没有立即显示?

如何使用数据注释为模型属性 WebApi .NET Core 添加布尔验证

在 spring-data-jpa 中通过布尔属性查询而不使用方法参数

Asp.net Core 模型绑定器接受布尔类型的随机整数

php中设置一个常量的日期值格式。 这样可以使用data()函数预期做比较,得到一个布尔值。。

比较具有布尔属性的两个通用项,如果其中任何一个返回true,则返回结果通用列表中的true