1

我有一个使用 GPS 并在某些标签上显示实际位置的应用程序。以下是更新位置的方法:

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    NSLog(@"didUpdateToLocation: %@", newLocation);
    CLLocation *currentLocation = newLocation;

    if (currentLocation != nil) {
        longitude.text = [NSString stringWithFormat:@"%.3f", currentLocation.coordinate.longitude];
        latitude.text = [NSString stringWithFormat:@"%.3f", currentLocation.coordinate.latitude];
    }

    NSLog(@"Resolving the Address");
    [geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error) {
        NSLog(@"Found placemarks: %@, error: %@", placemarks, error);
        if (error == nil && [placemarks count] > 0) {
            placemark = [placemarks lastObject];
            [address sizeToFit];
            NSArray *locationArray = [[NSArray alloc] initWithObjects:placemark.thoroughfare,placemark.subThoroughfare,
                                      placemark.postalCode,placemark.locality,placemark.country, nil];
            address.text = [NSString stringWithFormat:@"%@, %@\n%@ %@\n%@",
                                 [locationArray objectAtIndex:0],
                                 [locationArray objectAtIndex:1],
                                 [locationArray objectAtIndex:2],
                                 [locationArray objectAtIndex:3],
                                 [locationArray objectAtIndex:4]];
        } else {
            NSLog(@"%@", error.debugDescription);
        }
    } ];

}

现在,有时'locationArray' 的某些对象是'null',而相关标签在应用程序上显示为'(null)',这不太好。所以我需要一个'if'循环来检查'locationArray'的对象是否为'null',如果是,则不会显示。有任何想法吗?

更新

我解决了删除数组并使用@trojanfoe 的方法(稍作修改)的问题。这是代码:

- (NSString *)sanitizedDescription:(NSString *)obj {
    if (obj == nil)
    {
        return @"";
    }
    return obj;
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    //NSLog(@"didUpdateToLocation: %@", newLocation);
    CLLocation *currentLocation = newLocation;

    if (currentLocation != nil) {
        longitude.text = [NSString stringWithFormat:@"%.3f", currentLocation.coordinate.longitude];
        latitude.text = [NSString stringWithFormat:@"%.3f", currentLocation.coordinate.latitude];
    }

    NSLog(@"Resolving the Address");
    [geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error) {
        //NSLog(@"Found placemarks: %@, error: %@", placemarks, error);
        if (error == nil && [placemarks count] > 0) {
            placemark = [placemarks lastObject];
            [address sizeToFit];

            address.text = [NSString stringWithFormat:@"%@, %@\n%@ %@\n%@",
                            [self sanitizedDescription:placemark.thoroughfare],
                            [self sanitizedDescription:placemark.subThoroughfare],
                            [self sanitizedDescription:placemark.postalCode],
                            [self sanitizedDescription:placemark.locality],
                            [self sanitizedDescription:placemark.country]];
        } else {
            NSLog(@"%@", error.debugDescription);
        }
    } ];

}

非常感谢大家的帮助:)

4

2 回答 2

1

您必须创建一个辅助方法,用于测试NSNull该类并执行不同的操作:

- (NSString *)sanitizedDescription:(id)obj {
    if ([obj isKindOfClass:[NSNull class]]) {
        return @"";
    }
    return [obj description];
}

然后调用它而不是description直接调用:

address.text = [NSString stringWithFormat:@"%@, %@\n%@ %@\n%@",
                   [self sanitizedDescription:[locationArray objectAtIndex:0]],
                   [self sanitizedDescription:[locationArray objectAtIndex:1]],
                   [self sanitizedDescription:[locationArray objectAtIndex:2]],
                   [self sanitizedDescription:[locationArray objectAtIndex:3]],
                   [self sanitizedDescription:[locationArray objectAtIndex:4]]];

注意:此方法不必是 inself也不必是实例方法,它可以作为类方法正常工作。也许创建一个充满有用的类方法的帮助类?

于 2013-11-06T15:43:21.070 回答
0

查看您正在使用的代码NSArray,您声称它持有 NULL 这是错误的。NSArray 不能保存 NULL 指针。这里肯定有其他问题。

如果您真的想将 NULL 值存储在 Objective-C 中的数组中,您需要使用直接 C 数组或使用NSPointerArray. 否则,您需要使用[NSNull null]NSArray 来表示 NULL 值(正如其他人指出的那样)。

做最适合您的需求和设计的。请注意 NSPointerArray 在 iOS 6.0 或更高版本中更容易使用(您标记为 iOS 7 所以应该没问题)。根据您的需要,指针数组可以与强或弱 ARC 语义一起使用。

于 2013-11-06T16:25:00.400 回答