iOS - 为什么当我用“=="比较两个 NSNumber 时它会起作用?

iOS - Why Does It Work When I Compare Two NSNumbers With quot;==quot;?(iOS - 为什么当我用“==比较两个 NSNumber 时它会起作用?)

本文介绍了iOS - 为什么当我用“=="比较两个 NSNumber 时它会起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我在比较两个 NSNumber 对象时不小心使用了==",如下所示:

In my app, I accidentally used "==" when comparing two NSNumber objects like so:

NSNumber *number1;
NSNumber *number2;

后来,在设置了这些对象的 int 值之后,我不小心做了这个:

Later on, after these objects' int values were set, I accidentally did this:

if (number1 == number2) {
    NSLog(@"THEY'RE EQUAL");
}

而且,令人困惑的是,它奏效了!我可以发誓我被教导这样做:

And, confusingly, it worked! I could have sworn I was taught to do it this way:

if (number1.intValue == number2.intValue) {
    NSLog(@"THEY'RE EQUAL");
}

在两个 NSNumber 对象之间使用=="是如何工作的,为什么?这是否意味着可以这样比较它们,还是只是侥幸,通常不能保证每次都有效?这真的让我很困惑:(

How did using "==" between the two NSNumber objects work, and why? Does that mean it's okay to compare them that way, or was it just a fluke and this is generally not guaranteed to work every time? It really confused me :(

推荐答案

这可能是侥幸.

来自 NSHipster:

如果两个对象共享一组共同的可观察属性,则它们可以彼此相等或等价.然而,这两个对象可能仍然被认为是不同的,每个对象都有自己的身份.在编程中,对象的身份与其内存地址相关联.

Two objects may be equal or equivalent to one another, if they share a common set of observable properties. Yet, those two objects may still be thought to be distinct, each with their own identity. In programming, an object’s identity is tied to its memory address.

您的语句评估为 YES 可能是因为 number1number2 指向同一个对象.如果它们具有相同的值但是两个不同的对象,这将不起作用.

Its possible that your statement evaluated to YES because number1 and number2 were pointing to the same object. This would not work if they had the same value but were two different objects.

NSNumber 变量指向相同的明显原因是您明确地将一个分配给另一个,如下所示:

The obvious reason NSNumber variables would point to the same would be that you explicitly assigned one to the other, like so:

number1 = number2;

但还有另一件事.来自这个答案:

But there's one other thing. From this answer :

这可能是编译器优化或实现细节:由于 NSNumber 是不可变的,因此不需要它们是单独的实例.可能是考虑它的实现优化.可能 numberWithInt 在随后使用相同的整数调用时返回一个单例.

This is likely either a compiler optimisation or an implementation detail: as NSNumber is immutable there's no need for them be separate instances. probably an implementation optimisation thinking about it. Likely numberWithInt returns a singleton when called subsequently with the same integer.

但无论如何,使用 isEqualToNumber: 是最安全的,因为不知道还有哪些事物"潜伏在代码的深处,可能会或可能不会导致它评估 YES

But anyways, its safest to use isEqualToNumber:, as there is no telling what other "things" are lurking in the depths of code that may or may not cause it to evaluate YES

来自 RyPress:

虽然可以直接比较 NSNumber 指针,但 isEqualToNumber: 方法是一种更可靠的检查相等性的方法.它保证两个值比较相等,即使它们存储在不同的对象中.

While it’s possible to directly compare NSNumber pointers, the isEqualToNumber: method is a much more robust way to check for equality. It guarantees that two values will compare equal, even if they are stored in different objects.

这篇关于iOS - 为什么当我用“=="比较两个 NSNumber 时它会起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:iOS - 为什么当我用“=="比较两个 NSNumber 时它会起作用?

基础教程推荐