比较 NSNumber 和 NSInteger

发布于 2024-10-25 14:44:26 字数 704 浏览 1 评论 0原文

今天我花了一些时间追查两个错误,并最终使用相同的解决方案修复了这两个错误。

现在我已经有了解决方案,我希望能对其背后有一些清晰的认识。

我正在将核心数据(整数 16/NSNumber)中的属性与整数(ABPropertyID 和 ABMultiValueIdentifier)进行比较。

这个错误就在这个比较中,奇怪的是,只有在我杀死该应用程序(从后台托盘)、重新打开它并运行包含比较的相同过程后才出现。无论如何...

这就是重新启动后停止工作的原因:

if (myNumber.aProperty == [NSNUmber numberWithInt:anInteger]) { /* do stuff here */ }

这是两个解决方案,到目前为止,工作正常:

if ([myNumber.aProperty integerValue] == anInteger) {/* do stuff here */ }

if ([myNumber.aProperty isEqualToNumber:[NSNumber numberWithInt] :anInteger]]) { /* do stuff here */ }

对我来说,它们看起来都一样。我总是要么将 NSNumber 转换为整数值,要么将整数转换为 NSNumber。

有什么想法吗?

I spent some time today chasing down two bugs, and ended up fixing both of them using the same solution.

Now that I have the solution, I was hoping to get some clarity behind it.

I'm comparing an attribute from Core Data (Integer 16/NSNumber) with an Integer (ABPropertyID & ABMultiValueIdentifier).

The bug was in this comparison, and oddly enough, only showed itself after I had killed the app (from the background tray), reopened it, and run through the same process that included the comparison. Anyways...

This is what stopped working after a restart:

if (myNumber.aProperty == [NSNUmber numberWithInt:anInteger]) { /* do stuff here */ }

And these are the two solutions, which so far, are working perfectly:

if ([myNumber.aProperty integerValue] == anInteger) {/* do stuff here */ }

if ([myNumber.aProperty isEqualToNumber:[NSNumber numberWithInt:anInteger]]) { /* do stuff here */ }

To me, they all look identical. I'm always either converting the NSNumber to an integerValue, or converting the integer to an NSNumber.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

幸福不弃 2024-11-01 14:44:38

不要使用 == 来比较 NSNumber。大多数时候,您将比较两个不同的对象,因此比较的结果不会为 true。如果您查看 if 条件,请注意您特别将您的属性与全新 NSNumber 对象进行比较。

由于 NSInteger 是某些值类型的 Cocoa 包装器,因此将 NSInteger== 进行比较效果很好。

isEqualToNumber: 的实现可能也会采用包装的值类型并对它们进行比较。

Do not use == to compare NSNumbers. Most of the time you'll be comparing two distinct objects, so the comparison won't evaluate to true. If you look at your if condition, notice that you're particularly comparing your property to a brand new NSNumber object.

Since NSInteger is a Cocoa wrapper for certain value types, comparing NSIntegers with == works fine.

The implementation of isEqualToNumber: probably takes the wrapped value types and compares them too.

醉城メ夜风 2024-11-01 14:44:38

正如您所说,两种解决方案都有效...

我更喜欢第一个,因为它看起来更具可读性,恕我直言...
在将 NSNumber 转换为 int 后,当您比较整数时,它的性能也可能会更高。

在第二个中,您将 int 转换为对象,然后比较这两个对象......
这是第二个方法调用,在第一种情况下您没有...

希望这有帮助...:)

As you said, both solutions are working...

I would prefer the first one, as it appears more readable, IMHO...
It may also be more performant, as you are comparing integers, after having converted a NSNumber to an int.

In the second one, you convert an int to an object, then you compare the two objects...
So that's a second method call, which you don't have in the first case...

Hope this helps... : )

晒暮凉 2024-11-01 14:44:38

netWorkingButtonsIndexes 是保存对象的数组,
LinkedIn 是一个 int 数据类型的数字。

[[netWorkingButtonsIndexes objectAtIndex:buttonIndex] isEqual:[NSNumber numberWithInteger:LinkedIn]] 

通过使用 isEqual 方法,我们可以将对象与任何数据类型进行比较。

netWorkingButtonsIndexes is the array which holds objects and
LinkedIn is a number with int data type.

[[netWorkingButtonsIndexes objectAtIndex:buttonIndex] isEqual:[NSNumber numberWithInteger:LinkedIn]] 

By using the isEqual method we can compare objects with any data rtype.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文