简体   繁体   English

tableView中奇怪的BOOL行为(何时1不等于1?)

[英]Strange BOOL behavior in tableView (When is 1 not equal to 1?)

I'm trying to convert an existing app to use core data. 我正在尝试将现有应用程序转换为使用核心数据。 I've set up my entities, and am using an NSFetchedResultsController to display data in a table view. 我已经设置了实体,并且正在使用NSFetchedResultsController在表视图中显示数据。 For now, I'm just populating the database each time the app launches. 现在,我只是在每次启动应用程序时填充数据库。

It appears to be working fine, aside from one strange issue. 除了一个奇怪的问题之外,它似乎运行良好。 My entity has a BOOL value, "showStarCorner" that determines whether or not an image of a star should be shown in the table cell... so, I call something similar to this if statement when configuring each tableview cell: 我的实体具有BOOL值“ showStarCorner”,该值确定是否应在表格单元格中显示星星的图像...因此,在配置每个表格视图单元格时,我调用类似于以下if语句的东西:

if (myObject.showStarCorner == [NSNumber numberWithBool:YES]) {
    // show the star corner
}

But surprisingly that results in the star only being shown on the objects I created on that launch of the app. 但是令人惊讶的是,这导致星星只显示在我在应用启动时创建的对象上。 (objects added on previous launches of the app that should be showing the star are not) (在应用的先前启动中添加的应该显示星星的对象没有)

I added an extra test, to help figure out what is wrong: 我添加了一个额外的测试,以帮助找出问题所在:

if (myObject.showStarCorner == [NSNumber numberWithBool:YES]) {
    NSLog(@"%@ == %@", myObject.showStarCorner, [NSNumber numberWithBool:YES]);
}
else if (myObject.showStarCorner != [NSNumber numberWithBool:YES]) {
    NSLog(@"%@ != %@", myObject.showStarCorner, [NSNumber numberWithBool:YES]);
}

... which results in the else statement getting called for the previously added objects: ...这将导致为先前添加的对象调用else语句:

1 != 1

As far as I know, 1 should always equal 1... so I'm completely baffled. 据我所知,1应该总是等于1 ...所以我完全困惑。

I would greatly appreciate any help. 我将不胜感激任何帮助。 Could it have something to do with the new objects being created having the same attributes on each app launch? 可能与每次应用启动时创建的具有相同属性的新对象有关吗? (I'm not assigning any unique ID or anything, just sorting the results by the date they were added) (我没有分配任何唯一的ID或任何东西,只是按添加日期对结果进行排序)

NSNumber* variables are pointers, and == on pointers checks for pointer equality. NSNumber*变量是指针,指针上的==检查指针是否相等。 Since your objects don't share the same memory location (as they were created by distinct calls to [NSNumber numberWithBool:] ), they are not considered pointer-equal (and therefore the comparison returns NO ). 由于您的对象不共享相同的内存位置(因为它们是通过[NSNumber numberWithBool:]的不同调用创建的),因此它们不被视为等于指针的指针(因此,比较返回NO )。

Use the isEqual: method instead, which should check for value equality and is available on most classes, or get the boolValue out of your NSNumber objects and compare it. 请改用isEqual:方法,该方法应检查值是否相等并且在大多数类中都可用,或者从您的NSNumber对象中获取boolValue并进行比较。

if (myObject.showStarCorner.boolValue == YES) {
    // show the star corner
}

if ([myObject.showStarCorner isEqual:[NSNumber numberWithBool:YES]]) {
    // show the star corner
}

Objects, just because they point to the same data, are not guaranteed to be equal. 不能仅仅因为对象指向相同的数据就保证它们相等。 You need to use the -isEqual: method to test for equality, not ==: 您需要使用-isEqual:方法测试是否相等,而不是==:

if ([myObject.showStarCorner isEqual: [NSNumber numberWithBool: YES]) {
    //show the star corner
}

Compare the BOOL primitives: 比较BOOL原语:

if ([myObject.showStarCorner boolValue] == YES) {

// Alternatively,
if ([myObject.showStarCorner boolValue]) {

Otherwise you're trying to compare memory addresses of distinct NSNumber objects, which doesn't work. 否则,您将尝试比较不同的NSNumber对象的内存地址,这是行不通的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM