0

我有一个数组,其中包含当前用户位置和一些注释引脚之间的距离。尽管第一个日志正确显示了值,但当我将它们添加到数组中时,所有值都是“0”。我究竟做错了什么?我已经提到了苹果文档。我没有任何错误。先感谢您..

 for (Posts *post in allPosts) {
    CLLocation *postLocation = [[CLLocation alloc] initWithLatitude:post.coordinate.latitude longitude:post.coordinate.longitude];

    CLLocation *userLoc = [[CLLocation alloc] initWithLatitude:appDelegate.currentLocation.coordinate.latitude longitude:appDelegate.currentLocation.coordinate.longitude];

    CLLocationDistance dist = [userLoc distanceFromLocation:postLocation];
    distance = [NSString stringWithFormat:@"%.2f", dist];


    NSMutableArray *array = [[NSMutableArray alloc] initWithObjects:post, nil];

    NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:distance ascending:YES];
    NSLog(@"%@", sort);
    NSArray *sor = @[sort];
    NSArray *sorted = [array sortedArrayUsingDescriptors:sor];



    float flo = [[NSString stringWithFormat:@"%@",sorted]floatValue];
    NSLog(@"%.f",flo);
4

1 回答 1

0
float flo = [[NSString stringWithFormat:@"%@",sorted]floatValue];
NSLog(@"%.f",flo);

您正在尝试将数组放入字符串中,然后使用它的浮点值。您可能想要取而代之的是数组的一个元素。

于 2012-10-28T22:11:38.653 回答