Objective c locationServicesEnabled==是,但[[CLLocationMagager alloc]init]返回零
我有点困惑。考虑下面的代码:Objective c locationServicesEnabled==是,但[[CLLocationMagager alloc]init]返回零,objective-c,ios,cocoa-touch,cllocationmanager,Objective C,Ios,Cocoa Touch,Cllocationmanager,我有点困惑。考虑下面的代码: if([CLLocationManager locationServicesEnabled]) { if(self.locationManager == nil) { self.locationManager = [[CLLocationManager alloc] init]; } // do something
if([CLLocationManager locationServicesEnabled])
{
if(self.locationManager == nil)
{
self.locationManager = [[CLLocationManager alloc] init];
}
// do something with the location manager...
}
self.locationManager
设置为0x0。两个备选方案的条件都满足。我在self.locationManager=…
上使用断点检查了这一点,并且在每次启动应用程序时都会调用该行。最有趣的是,同一视图控制器中的MKMapView能够显示用户的位置
谢谢你的见解,
Chris问题在于将
locationManager
声明为
@property(weak, nonatomic) CLLocationManager* locationManager;
框架没有保留每个location manager实例,因此我应该声明
@property(strong, nonatomic) CLLocationManager* locationManager;
相反,它工作得非常好
感谢LocoMike为我指明了正确的方向。你说“每次启动应用程序”。当你的应用程序启动时,这难道不是你所期望的对象为零吗?你如何定义locationManager属性?我还假设您正在导入#导入并添加该框架,对吗?@onnoweb:
self.locationManager
在init
方法中初始化为nil。稍后,viewDidLoad
@LocoMike:@property(弱、非原子)CLLocationManager*locationManager调用上述代码代码>@LocoMike:将财产所有权关系从弱变强成功了,谢谢。(由于我是新用户,我可能在接下来的7小时内不会回答自己的问题…)