无法识别的选择器发送到合成属性上的实例
让我先声明一下,我是 Objective-C/iOS 的新手。
我的程序因未捕获的异常 NSInvalidArgumentException 崩溃,原因:[CLLocationManager copyWithZone:]: 无法识别的选择器发送到实例。
这似乎是一个非常常见的错误,我可以说,它通常发生在以下情况:内存管理方面出了问题。我在 stackoverflow 和 Google 上看过类似的问题,但似乎没有一个完全相同。
我的应用程序是一个简单的单视图应用程序。我正在尝试使用 CLLocationManager 类,因为我想获取用户的标题。我的代码:
magnetoTestViewController.h
#import <UIKit/UIKit.h>
@class CLLocationManager;
@interface magnetoTestViewController : UIViewController
@property(copy, readwrite) CLLocationManager *locManager;
@end
magnetoTestViewController.m
#import "magnetoTestViewController.h"
#import <CoreLocation/CoreLocation.h>
@interface magnetoTestViewController()
- (void)startHeadingEvents;
@end
@implementation magnetoTestViewController
@synthesize locManager = _locManager;
...
- (void)startHeadingEvents {
NSLog(@"entered startHeadingEvents()");
if (!self.locManager) {
CLLocationManager* theManager = [[CLLocationManager alloc] init];
// Retain the object in a property.
self.locManager = theManager;
self.locManager.delegate = self;
}
// Start location services to get the true heading.
self.locManager.distanceFilter = 1000;
self.locManager.desiredAccuracy = kCLLocationAccuracyKilometer;
[self.locManager startUpdatingLocation];
// Start heading updates.
if ([CLLocationManager headingAvailable]) {
NSLog(@"Yep, the heading is available.");
self.locManager.headingFilter = 5;
[self.locManager startUpdatingHeading];
}
else {
NSLog(@"*sadface*, the heading information is not available.");
}
NSLog(@"exited startHeadingEvents()");
}
- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading {
NSLog(@"locationManagerdidUpdateHeading() was called.");
if (newHeading.headingAccuracy < 0) {
NSLog(@"the heading accuracy is smaller than 0. returning.");
return;
}
// Use the true heading if it is valid.
CLLocationDirection theHeading = ((newHeading.trueHeading > 0) ?
newHeading.trueHeading : newHeading.magneticHeading);
NSString* myNewString = [NSString stringWithFormat:@"the heading is %d", theHeading];
NSLog(myNewString);
}
我的代码正在进入 startHeadingEvents
方法(基于我的日志记录),但在退出该方法之前崩溃(基于我的日志未被调用)。我假设copyWithZone(在错误中)是在某个时刻内部调用的 CLLocationManager 的一种方法。我确定我在某个地方犯了业余错误,有人可以指出我正确的方向吗?
Let me qualify by saying I'm new to Objective-C/iOS.
My program is crashing on the uncaught exception NSInvalidArgumentException, reason: [CLLocationManager copyWithZone:]: unrecognized selector sent to instance.
This seems to be a pretty common error, and best I can tell, it usually happens when something's gone wrong memory-management wise. I've looked at similar questions on stackoverflow and Google, but none seem quite the same.
My application is a simple single view app. I'm trying to use the CLLocationManager
class, because I want to get the user's heading. My code:
magnetoTestViewController.h
#import <UIKit/UIKit.h>
@class CLLocationManager;
@interface magnetoTestViewController : UIViewController
@property(copy, readwrite) CLLocationManager *locManager;
@end
magnetoTestViewController.m
#import "magnetoTestViewController.h"
#import <CoreLocation/CoreLocation.h>
@interface magnetoTestViewController()
- (void)startHeadingEvents;
@end
@implementation magnetoTestViewController
@synthesize locManager = _locManager;
...
- (void)startHeadingEvents {
NSLog(@"entered startHeadingEvents()");
if (!self.locManager) {
CLLocationManager* theManager = [[CLLocationManager alloc] init];
// Retain the object in a property.
self.locManager = theManager;
self.locManager.delegate = self;
}
// Start location services to get the true heading.
self.locManager.distanceFilter = 1000;
self.locManager.desiredAccuracy = kCLLocationAccuracyKilometer;
[self.locManager startUpdatingLocation];
// Start heading updates.
if ([CLLocationManager headingAvailable]) {
NSLog(@"Yep, the heading is available.");
self.locManager.headingFilter = 5;
[self.locManager startUpdatingHeading];
}
else {
NSLog(@"*sadface*, the heading information is not available.");
}
NSLog(@"exited startHeadingEvents()");
}
- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading {
NSLog(@"locationManagerdidUpdateHeading() was called.");
if (newHeading.headingAccuracy < 0) {
NSLog(@"the heading accuracy is smaller than 0. returning.");
return;
}
// Use the true heading if it is valid.
CLLocationDirection theHeading = ((newHeading.trueHeading > 0) ?
newHeading.trueHeading : newHeading.magneticHeading);
NSString* myNewString = [NSString stringWithFormat:@"the heading is %d", theHeading];
NSLog(myNewString);
}
My code is entering the startHeadingEvents
method (based on my logging) but is crashing before exiting the method (based on my logging not being called). I assume copyWithZone
(which is in the error) is a method of the CLLocationManager being called internally at some point. I'm sure I'm making an amateur mistake somewhere, can someone point me in the right direction?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的问题是您在 CLLocationManager 的属性中使用“复制”,这是一个单例 - 通常单例被定义为抛出异常以防止复制单个实例。
相反,像这样声明你的财产:
Your problem is that you are using "copy" in your property for the CLLocationManager, which is a singleton - often Singletons are defined such that they throw an exception to prevent copying the single instance.
Instead, declare your property like so: