我有一个数组,其中包含当前用户位置和一些注释引脚之间的距离。虽然第一个日志正确显示了值,但是当我在数组中添加它们时,所有值都是“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);
答案 0 :(得分:0)
float flo = [[NSString stringWithFormat:@"%@",sorted]floatValue];
NSLog(@"%.f",flo);
您正在尝试将数组放入字符串中,然后使用它的浮点值。你可能想要取一个数组的元素。