为什么这个简单的\'if\'语句不起作用(在快速枚举中)?

| 我通过表中的
ChecklistItem
实体进行枚举,以查看哪个具有
priority
(NSNumber属性)为1的实体。
checklistItems
Checklist
有很多关系。 在这个简单的代码中,第一个NSLog可以正常工作,并报告我的几个ChecklistItem的优先级为1。但是第二个NSLog从未被调用。为什么是这样?我假设我错误地将“ if”语句置为错误,但是我不知道该怎么做。
for (ChecklistItem *eachItem in checklist.checklistItems){
    NSLog(@\"Going through loop. Item %@ has priority %@.\", eachItem.name, eachItem.priority);

    if (eachItem.priority == [NSNumber numberWithInt:1]) {
        NSLog(@\"Item %@ has priority 1\", eachItem.name);
        }
}
    
已邀请:
您不能像上面那样比较对象。使用以下代码。
for (ChecklistItem *eachItem in checklist.checklistItems){
    NSLog(@\"Going through loop. Item %@ has priority %@.\", eachItem.name, eachItem.priority);

    if ([eachItem.priority intValue]== 1) {
        NSLog(@\"Item %@ has priority 1\", eachItem.name);
        }
}
谢谢,     
您正在比较返回值
eachItem.priority
[NSNumber numberWithInt:1]
的指针。您应使用ѭ8的相等方法。     
好吧,您应该检查像这样的值相等性:
if ( [eachItem.priority intValue] == 1 ) { ... }
但是,我很惊讶它并不会按原样工作,因为我认为
NSNumber
汇集了一些基本实例,我希望1是其中之一。即使在这种情况下,依靠它也将是非常糟糕的形式。     

要回复问题请先登录注册