代码适用于64位iPhone而不是其他任何人...为什么不呢?

时间:2014-07-04 08:12:27

标签: ios objective-c algorithm

经过实验,我意识到这段代码只适用于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);
    }];
}

2 个答案:

答案 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的修订版