如果未启用服务,则释放实例?

时间:2010-05-19 12:46:02

标签: iphone objective-c core-location

我想检查一下我是否正确,我正在创建CCLocationManager的实例,然后检查是否启用了位置服务。如果未启用,我会报告错误,释放实例并继续,看起来/听起来是否合适?

locationManager = [[CLLocationManager alloc] init];
BOOL supportsService = [locationManager locationServicesEnabled];

if(supportsService) {
    [locationManager setDelegate:self];
    [locationManager setDistanceFilter:kCLDistanceFilterNone];
    [locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
    [locationManager startUpdatingLocation];
} else {
    NSLog(@"Location services not enabled.");
    [locationManager release];
}
... 
...
... more code
欢呼加里

1 个答案:

答案 0 :(得分:2)

为了防止错误进一步降低,您可以在释放后将locationManager设置为nil。除此之外,您的方法没有问题。