Ios 扩展CLPlacemark会导致EXC访问错误

Ios 扩展CLPlacemark会导致EXC访问错误,ios,objective-c,core-location,extend,clplacemark,Ios,Objective C,Core Location,Extend,Clplacemark,虽然发现了一个类似的问题,但它没有提供答案,至少对一般问题没有 我的问题是: 由于CoreLocation地理编码是有速率限制的,而我正在为其开发一个应用程序的(web-)服务提供自己的备用地理编码服务,我想在达到苹果速率限制的情况下使用这个自定义地理编码服务。此外,我觉得完全有必要为这个自定义RESTAPI返回的结果避免自定义数据类型,因此希望使用返回的数据生成CLPlacemarks。但是,文档说明CLPlacemark属性,如位置、位置、管理区域等是只读的。 因此,我创建了一个子类CLPl

虽然发现了一个类似的问题,但它没有提供答案,至少对一般问题没有

我的问题是: 由于
CoreLocation
地理编码是有速率限制的,而我正在为其开发一个应用程序的(web-)服务提供自己的备用地理编码服务,我想在达到苹果速率限制的情况下使用这个自定义地理编码服务。此外,我觉得完全有必要为这个自定义RESTAPI返回的结果避免自定义数据类型,因此希望使用返回的数据生成
CLPlacemark
s。但是,文档说明
CLPlacemark
属性,如
位置、位置、管理区域
等是
只读的
。 因此,我创建了一个子类
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文件中的数据,并使用数据调用my
initWithLocation:Location:administrativeArea:country:
方法,结果在测试结束时(在测试方法结束时)出现
EXC错误访问(code=1)
placemark变量指向
nil
,尽管前面的
NSLog(@“placemark:%@”,customPlacemark)输出正确的值。此外,逐行通过测试显示
CustomPlacemark
工作(即指向正确填充的对象),直到测试结束。对我来说,这表明我的
CustomPlacemark
解除分配出了问题,但具体是什么


非常感谢您的帮助

作为对任何在这里遇到类似问题的人的参考:

在谷歌进行了一番密集的Fu搜索并深入苹果的信息源之后,似乎扩展
CLPlacemark
是不可取的

然而,我能够基于找到的技巧实现一种变通方法,它基本上滥用了
MKPlacemark
扩展
CLPlacemark
的事实,并提供了一种使用自定义数据初始化的方法,即
-(instancetype _Nonnull)initWithCoordinate:(CLLocationCoordinate2D)坐标地址字典:(NSDictionary*_Nullable)addressDictionary
。为
addressDictionary
查找正确的键以映射
CLPlacemark
中所需的属性可能需要一些尝试和错误,特别是因为iOS 9已经不推荐使用
ABPerson/Address
功能。 我为我的目的找到的钥匙是:

@"City" -> CLPlacemark.city
@"State" -> CLPlacemark.administrativeArea
@"Country" -> CLPlacemark.country
谢谢@hpd

我的swift模拟课:

class TestPlacemark: CLPlacemark {
    static let coordinate = CLLocationCoordinate2D(latitude: CLLocationDegrees(35), longitude: CLLocationDegrees(-80))

    override init() {
        let mkPlacemark = MKPlacemark(coordinate: TestPlacemark.coordinate) as CLPlacemark
        super.init(placemark: mkPlacemark)
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override static var supportsSecureCoding: Bool {
        return false
    }
    override var location: CLLocation? {
        return CLLocation(latitude: TestPlacemark.coordinate.latitude, longitude: TestPlacemark.coordinate.longitude)
    }
    override var postalAddress: CNPostalAddress?  {
        return TestPostalAddress()
    }
}
希望它能为其他人节省一些时间。干杯