根据==,从同一个float创建的两个NSNumber不相等

时间:2011-03-14 12:33:29

标签: objective-c cocoa-touch comparison nsnumber

我有几个float我将其转换为NSNumber并添加到数组中,然后按数字顺序排序。

然后我将原始的float转换为NSNumber s,但如果我尝试将它们if(numinarray = newnum){进行比较,那么它永远不会去任何地方。当我NSLog()输出值时,值匹配:

float distance  = [barnardCastle distanceFromLocation:loc1] / 1000;
float distance2 = [billingham distanceFromLocation:loc1] / 1000;
float distance3 = [bishopAukland distanceFromLocation:loc1] / 1000;
float distance4 = [cockerton distanceFromLocation:loc1] / 1000;
float distance5 = [darlington distanceFromLocation:loc1] / 1000;
float distance6 = [guisborough distanceFromLocation:loc1] / 1000;
float distance7 = [hartlepool distanceFromLocation:loc1] / 1000;
float distance8 = [middlesbrough distanceFromLocation:loc1] / 1000;
float distance9 = [northallerton distanceFromLocation:loc1] / 1000;
float distance10 = [redcar distanceFromLocation:loc1] / 1000;
float distance11 = [stockton distanceFromLocation:loc1] / 1000;
float distance12 = [yarm distanceFromLocation:loc1] / 1000;


NSArray *nums, *sorted;

nums = [NSArray arrayWithObjects:
        [NSNumber numberWithFloat:distance],
        [NSNumber numberWithFloat:distance2],
        [NSNumber numberWithFloat:distance3],
        [NSNumber numberWithFloat:distance4],
        [NSNumber numberWithFloat:distance5],
        [NSNumber numberWithFloat:distance6],
        [NSNumber numberWithFloat:distance7],
        [NSNumber numberWithFloat:distance8],
        [NSNumber numberWithFloat:distance9],
        [NSNumber numberWithFloat:distance10],
        [NSNumber numberWithFloat:distance11],
        [NSNumber numberWithFloat:distance12],
        nil];

sorted = [nums sortedArrayUsingSelector:@selector(compare:)];

NSLog(@"nums sorted");
NSLog(@"distance is %f",distance);
NSNumber *intdist = [NSNumber numberWithFloat:distance]; 
NSNumber *intdist2 = [NSNumber numberWithFloat:distance2];
NSNumber *intdist3 = [NSNumber numberWithFloat:distance3];
NSNumber *intdist4 = [NSNumber numberWithFloat:distance4];
NSNumber *intdist5 = [NSNumber numberWithFloat:distance5];
NSNumber *intdist6 = [NSNumber numberWithFloat:distance6];
NSNumber *intdist7 = [NSNumber numberWithFloat:distance7];
NSNumber *intdist8 = [NSNumber numberWithFloat:distance8];
NSNumber *intdist9 = [NSNumber numberWithFloat:distance9];
NSNumber *intdist10 = [NSNumber numberWithFloat:distance10];
NSNumber *intdist11 = [NSNumber numberWithFloat:distance11];
NSNumber *intdist12 = [NSNumber numberWithFloat:distance12];
NSLog(@"int dist %@",intdist);
NSLog(@"int dist2 %@",intdist2);
NSLog(@"int dist3 %@",intdist3);
NSLog(@"int dist4 %@",intdist4);
NSLog(@"int dist5 %@",intdist5);
NSLog(@"int dist6 %@",intdist6);
NSLog(@"int dist7 %@",intdist7);
NSLog(@"int dist8 %@",intdist8);
NSLog(@"int dist9 %@",intdist9);
NSLog(@"int dist10 %@",intdist10);
NSLog(@"int dist11 %@",intdist11);
NSLog(@"int dist12 %@",intdist12);


nearestLoc = [[NSMutableArray alloc]init];

NSLog(@"about to loop");
for (int i=0; i < 12; i++) {
    Branches *branches = [[Branches alloc]init];
    NSLog(@"looping");

    NSNumber *index = [sorted objectAtIndex:i];

    NSNumber *intdist = [NSNumber numberWithFloat:distance]; 
    NSNumber *intdist2 = [NSNumber numberWithFloat:distance2];
    NSNumber *intdist3 = [NSNumber numberWithFloat:distance3];
    NSNumber *intdist4 = [NSNumber numberWithFloat:distance4];
    NSNumber *intdist5 = [NSNumber numberWithFloat:distance5];
    NSNumber *intdist6 = [NSNumber numberWithFloat:distance6];
    NSNumber *intdist7 = [NSNumber numberWithFloat:distance7];
    NSNumber *intdist8 = [NSNumber numberWithFloat:distance8];
    NSNumber *intdist9 = [NSNumber numberWithFloat:distance9];
    NSNumber *intdist10 = [NSNumber numberWithFloat:distance10];
    NSNumber *intdist11 = [NSNumber numberWithFloat:distance11];
    NSNumber *intdist12 = [NSNumber numberWithFloat:distance12];
    NSLog(@"int dist %@",intdist);
    NSLog(@"int dist2 %@",intdist2);
    NSLog(@"int dist3 %@",intdist3);
    NSLog(@"int dist4 %@",intdist4);
    NSLog(@"int dist5 %@",intdist5);
    NSLog(@"int dist6 %@",intdist6);
    NSLog(@"int dist7 %@",intdist7);
    NSLog(@"int dist8 %@",intdist8);
    NSLog(@"int dist9 %@",intdist9);
    NSLog(@"int dist10 %@",intdist10);
    NSLog(@"int dist11 %@",intdist11);
    NSLog(@"int dist12 %@",intdist12);
    NSLog(@"index %@",index);


    if (index == intdist) {

        branches.branchLoc = @"Barnard Castle";
        branches.distance = distance;
        [nearestLoc addObject:branches];
        NSLog(@"adding barnard castle");
    }

    if (index == intdist2) {
        branches.branchLoc = @"Billingham";
        branches.distance = distance2;
        [nearestLoc addObject:branches];

        NSLog(@"adding billingham");
    }

我错过了使用NSNumber的内容吗?如果是这样,你会如何得到它?

2 个答案:

答案 0 :(得分:14)

不要使用==,而是拨打compare:isEqualToNumber:。当您使用==时,您正在比较指针。

答案 1 :(得分:10)

您正在使用compare指针而不是值的测试!

比较NSNumber应该用

进行
if([numInArray compare:newNum] == NSOrderedSame) {
    // do something
}