[英]How to compare NSNumber with a decimal value?
我最近從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];
}
對於任何可能關注此討論的人-這就是我最終得到的結果
- (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, @"");
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.