我有一个使用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);
}
} ];
}
非常感谢所有人的帮助:)。
答案 0 :(得分: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]]];
注意:此方法不必位于self
中,并且它不必是实例方法,它可以作为类方法正常工作。也许创建一个充满有用的类方法的辅助类?
答案 1 :(得分:0)
查看您的代码,您使用的是NSArray
,并声称它保留了NULL,这是错误的。 NSArray无法保存NULL指针。这里还有其他一些问题。
如果你真的想在objective-c中的数组中存储NULL值,你需要使用直的C数组或使用NSPointerArray
。否则,您需要使用[NSNull null]
来表示NSArray中的NULL值(正如其他人指出的那样)。
最适合您的需求和设计。请注意,NSPointerArray在iOS 6.0或更高版本中更容易使用(您标记为iOS 7,因此您应该没问题)。根据您的需要,指针数组可以与强ARC或弱ARC语义一起使用。