Objective-C,为什么这些NSArray不匹配?

时间:2021-04-25 19:58:50

I have two NSArrays that I'm comparing — in the NSLog output they look identical, yet they don't equal each other somehow. If I convert the NSArray to an NSString I get the same exact result. Comparing them to themselves will be equal. How can I determine why one and two aren't equal? thank you.

我有两个我正在比较的NSArrays - 在NSLog输出中它们看起来完全一样,但它们在某种程度上并不相同。如果我将NSArray转换为NSString,我会得到完全相同的结果。将它们与自己进行比较将是平等的。我怎样才能确定为什么一和二不相等?谢谢。

- (void)confused:(NSArray *)two {

    NSArray *one = [NSArray arrayWithObjects:@"16777223", @"7", nil];
    NSArray *two = [[NSBundle bundleWithPath:@"/path/to/bundle"] executableArchitectures];

    // NSArray "two" shows as 16277223, 7 in NSLog

    if ([two firstObjectCommonWithArray:(NSArray *)one])
    {
        NSLog(@"- it's equal %@ %@", one, two);
        // if array one matches array two then this will output
    }
    else {
        NSLog(@"- it's NOT equal %@ %@", one, two);
    }

    return;
}

Here's the output from console:

这是控制台的输出:

myApp (
    16777223,
    7
)
myApp (
    16777223,
    7
)
myApp - it's NOT equal (
    16777223,
    7
)(
    16777223,
    7
)

1 个解决方案

#1


1  

-[NSBundle executableArchitectures] returns an array of NSNumber objects, not NSString objects, so the array you're passing in doesn't have strings in it. If you change

- [NSBundle executableArchitectures]返回NSNumber对象的数组,而不是NSString对象,因此您传入的数组中没有字符串。如果你改变了

NSArray *one = [NSArray arrayWithObjects:@"16777223",@"7", nil];

to

NSArray *one = [NSArray arrayWithObjects:[NSNumber numberWithUnsignedInteger:NSBundleExecutableArchitectureX86_64], 
                                         [NSNumber numberWithUnsignedInteger:NSBundleExecutableArchitectureI386], 
                  nil];

your code should work.

你的代码应该工作。

#1


1  

-[NSBundle executableArchitectures] returns an array of NSNumber objects, not NSString objects, so the array you're passing in doesn't have strings in it. If you change

- [NSBundle executableArchitectures]返回NSNumber对象的数组,而不是NSString对象,因此您传入的数组中没有字符串。如果你改变了

NSArray *one = [NSArray arrayWithObjects:@"16777223",@"7", nil];

to

NSArray *one = [NSArray arrayWithObjects:[NSNumber numberWithUnsignedInteger:NSBundleExecutableArchitectureX86_64], 
                                         [NSNumber numberWithUnsignedInteger:NSBundleExecutableArchitectureI386], 
                  nil];

your code should work.

你的代码应该工作。