经过实验,我意识到这段代码只适用于64位iPhone,而不适用于其他iPhone。不是在iPad上,不是在我自己的设备上(iPhone 5)而不是在任何未指定为64位的东西上。为什么会这样?
- (void)loadVenues {
NSString *latLon = @"30, -120";
NSString *clientID = kCLIENTID;
NSString *clientSecret = kCLIENTSECRET;
NSDictionary *queryParams = @{@"ll" : latLon,
@"client_id" : clientID,
@"client_secret" : clientSecret,
@"categoryId" : @"4bf58dd8d48988d1e0931735",
@"v" : @"20140118"};
[[RKObjectManager sharedManager] getObjectsAtPath:@"/v2/venues/search"
parameters:queryParams
success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult)
{
_venues = mappingResult.array;
NSMutableArray *temp = [(NSArray*)_venues mutableCopy];
Venue *holder;
for (int i = 0; i < _venues.count; i++) {
for (int j = 0; j < i; j++) {
if (((Venue *) [temp objectAtIndex:i]).location.distance < ((Venue *) [temp objectAtIndex:j]).location.distance) {
holder = [temp objectAtIndex:j];
[temp replaceObjectAtIndex:j withObject:[temp objectAtIndex:i]];
[temp replaceObjectAtIndex:i withObject:holder];
}
}
}
_venues = temp;
[self.tableView reloadData];
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
NSLog(@"What do you mean by 'there is no coffee?': %@", error);
}];
}
答案 0 :(得分:0)
不是说我认为你的排序算法很糟糕,但为什么不使用内置的排序算法来简化你的代码呢?
NSMutableArray *arrayToSort = (NSMutableArray *)mappingResult.array;
[arrayToSort sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
Venue *venue1 = obj1;
Venue *venue2 = obj2;
double value1 = venue1.location.distance;
double value2 = venue2.location.distance;
if (value1 > value2)
return NSOrderedAscending;
else if (value1 < value2)
return NSOrderedDescending;
else
return NSOrderedSame;
}];
答案 1 :(得分:0)
好的,我想我知道问题是什么。你说.distance的类型为NSNumber
。所以你要做的就是比较两个对象。你想要的是比较两个浮点值。所以你应该修改你的if语句代码:
if (((Venue *) [temp objectAtIndex:i]).location.distance.floatValue < ((Venue *) [temp objectAtIndex:j]).location.distance.floatValue)
我还建议您使用@ doctordoder的修订版