如何比较NSNumber与小数值?

时间:2012-10-24 01:10:47

标签: objective-c ios6 nsnumber ocunit

我最近从iOS 5升级到iOS 6,发现此单元测试失败

- (void)testCalculatesDistanceBetweenTwoPoints
{
    self.sut = [[DistanceCalculator alloc] init];
    CLLocationCoordinate2D start = {.latitude = 34.32, .longitude = 99.13};
    CLLocationCoordinate2D finish = {.latitude = 105.94, .longitude = 27.73};
    NSNumber *distance = [self.sut kilometresBetweenPlace1:start andPlace2:finish];
    NSNumber *expected = [NSNumber numberWithDouble:3822.23073702318];
    STAssertEqualObjects(distance, expected, @"");
}

失败的断言

  

'3822.23073702318'应该等于'3822.23073702318'

当我打印每个数字的原始值时,我得到的内容看起来是相同的值

2012-10-23 20:01:42.970 HelloWorld[1573:c07] 1 3822.23073702318
2012-10-23 20:01:42.970 HelloWorld[1573:c07] 2 3822.23073702318

当我打印每个数字的类型时,我得到的内容看起来是一样的(我可能做错了吗?)

2012-10-23 20:06:37.309 HelloWorld[1611:c07] 1 __NSCFNumber 
2012-10-23 20:06:37.309 HelloWorld[1611:c07] 2 __NSCFNumber

如果有帮助,这是完整的实施

- (NSNumber *)kilometresBetweenPlace1:(CLLocationCoordinate2D)place1 andPlace2:(CLLocationCoordinate2D)place2
{
    MKMapPoint start = MKMapPointForCoordinate(place1);
    MKMapPoint finish = MKMapPointForCoordinate(place2);

    double distance = MKMetersBetweenMapPoints(start, finish) / 1600;

    return [NSNumber numberWithDouble:distance];
}

1 个答案:

答案 0 :(得分:0)

对于任何可能关注此讨论的人来说,这也是我最终的目标

- (void)testCalculatesDistanceBetweenTwoPoints
{
    double accuracy = 0.00000000001;
    self.sut = [[DistanceCalculator alloc] init];
    CLLocationCoordinate2D start = {.latitude = 34.32, .longitude = 99.13};
    CLLocationCoordinate2D finish = {.latitude = 105.94, .longitude = 27.73};
    NSNumber *distance = [self.sut kilometresBetweenPlace1:start andPlace2:finish];
    NSNumber *expected = [NSNumber numberWithDouble:3822.23073702318];
    STAssertEqualsWithAccuracy([distance doubleValue], [expected doubleValue], accuracy, @"");
}