【发布时间】:2015-12-16 15:02:03
【问题描述】:
虽然发现了一个类似的问题here,但它没有提供答案,至少不是针对一般问题。
我的问题是:
由于CoreLocation 地理编码受到速率限制,并且我正在为其开发应用程序的(网络)服务提供其自己的后备地理编码服务,因此我想使用此自定义地理编码服务以防达到 Apple 的速率限制。此外,我觉得避免自定义 REST API 返回的结果的自定义数据类型是完全有意义的,因此我想使用返回的数据来生成CLPlacemarks。但是,文档指出 CLPlacemark 属性(例如 location, locality, administrativeArea 等)是 read-only。
因此,我创建了一个 CLPlacemark 的子类,将所需的属性综合到我可以访问的私有变量上,即:
// interface: (.h)
@interface CustomPlacemark : CLPlacemark
- (nonnull id)initWithLocation: (nonnull CLLocation *)location
locality: (nullable NSString *)locality
administrativeArea: (nullable NSString *)adminArea
country: (nullable NSString *)country;
@end
// implementation (.m)
@implementation CustomPlacemark
@synthesize location = _location;
@synthesize locality = _locality;
@synthesize country = _country;
@synthesize administrativeArea = _administrativeArea;
- (nonnull id)initWithLocation: (nonnull CLLocation *)location
locality: (nullable NSString *)locality
administrativeArea: (nullable NSString *)adminArea
country: (nullable NSString *)country{
self = [super init];
if(self){
_location = location;
_locality = locality;
_administrativeArea = adminArea;
_country = country;
}
return self;
}
@end
使用单元测试测试此代码,该单元测试解析来自 JSON 文件的数据并调用我的 initWithLocation: locality: administrativeArea: country: 方法,数据在测试结束时生成 EXC BAD ACCESS (code=1)(在测试方法的结束 } ) 与地标变量指向 nil 尽管先前的 NSLog(@"placemark: %@", customPlacemark); 输出正确的值。此外,逐行执行测试显示CustomPlacemark 工作(即指向正确填充的对象)直到测试结束。对我来说,这表明我的 CustomPlacemark 的释放出现了问题 - 但究竟是什么?
非常感谢任何帮助!
【问题讨论】:
标签: ios objective-c core-location extend clplacemark