简体   繁体   English

将NSNumber与NSInteger进行比较

[英]Compare NSNumber with NSInteger

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). 我正在将Core Data(Integer 16 / NSNumber)的属性与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. 我总是要么将NSNumber转换为integerValue,要么将整数转换为NSNumber。

Any ideas? 有任何想法吗?

Do not use == to compare NSNumber s. 不要使用==来比较NSNumber 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. 如果您查看if条件,请注意您特别将您的属性与全新的 NSNumber对象进行比较。

Since NSInteger is a Cocoa wrapper for certain value types, comparing NSInteger s with == works fine. 由于NSInteger是某些值类型的Cocoa包装器,因此将NSInteger==进行比较可以正常工作。

The implementation of isEqualToNumber: probably takes the wrapped value types and compares them too. isEqualToNumber:的实现isEqualToNumber:可能采用包装的值类型并对它们进行比较。

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. 我更喜欢第一个,因为它看起来更具可读性,恕我直言......在将NSNumber转换为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... 在第二个中,你将一个int转换为一个对象,然后你比较两个对象......所以这是第二个方法调用,在第一种情况下你没有...

Hope this helps... : ) 希望这可以帮助... : )

netWorkingButtonsIndexes is the array which holds objects and LinkedIn is a number with int data type. netWorkingButtonsIndexes是保存对象的数组, LinkedIn是具有int数据类型的数字。

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

By using the isEqual method we can compare objects with any data rtype. 通过使用isEqual方法,我们可以将对象与任何数据rtype进行比较。

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

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