【发布时间】:2011-11-29 14:37:03
【问题描述】:
我在设备上使用CLLocationManager 并且没有调用委托方法。这是我的代码(我可以看到该类也没有被释放):
#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>
@interface CurrentLocation : NSObject <CLLocationManagerDelegate> {
CLLocationManager *locationManager;
}
@property (nonatomic, retain) CLLocationManager *locationManager;
@end
#import "CurrentLocation.h"
#import "SBJson.h"
@implementation CurrentLocation
@synthesize locationManager;
- (id)init
{
self = [super init];
if (self) {
NSLog(@"Initialized");
locationManager = [[[CLLocationManager alloc] init] autorelease];
locationManager.delegate = self;
[locationManager startUpdatingLocation];
BOOL enable = [CLLocationManager locationServicesEnabled];
NSLog(@"%@", enable? @"Enabled" : @"Not Enabled");
}
return self;
}
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation {
NSLog(@"A");
NSString *stringURL = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/geocode/json?latlng=%f,%f&sensor=true", newLocation.coordinate.latitude, newLocation.coordinate.longitude];
NSLog(@"%@", stringURL);
NSURL *url = [NSURL URLWithString:stringURL];
NSData *data = [NSURLConnection sendSynchronousRequest:[NSURLRequest requestWithURL:url] returningResponse:nil error:nil];
SBJsonParser *parser = [[SBJsonParser alloc] init];
NSDictionary *dict = [parser objectWithData:data];
NSLog(@"%@", dict);
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
NSLog(@"B");
}
- (void)dealloc {
NSLog(@"Dealloc called");
[super dealloc];
[locationManager release];
}
@end
在日志中,我看到:
2011-10-02 19:49:04.095 DixieMatTracker[1404:707] Initialized
2011-10-02 19:49:04.109 DixieMatTracker[1404:707] Enabled
我做错了什么? 谢谢
【问题讨论】:
-
locationManager属性是如何定义的? -
不太可能有所作为,但我见过更奇怪的事情。放置 self.locationManager (访问属性)。没有 self,它会访问支持变量,而不保留(因为您已经自动释放)。
-
@PragmaOnce 是
assign,retain,什么?尝试改用self.locationManager。 @Daryl Teo 我同意,这很可能是问题,因为locationManager将被自动释放。 -
[super dealloc]应该在您的自定义dealloc实现结束时调用,而不是首先调用。
标签: objective-c ios cocoa-touch cllocationmanager