Ios 如何同时符合MKAnnotation和Codable?

Ios 如何同时符合MKAnnotation和Codable?,ios,mapkit,mkannotation,swift5,codable,Ios,Mapkit,Mkannotation,Swift5,Codable,我想在地图上显示我的一个对象。因此,在类声明中,我使其符合MKAnnotation。符合MKAnnotation要求我添加类型为CLLocationCoordinate2D的坐标属性。但我希望以后能够将这些对象发送到Web服务器,因此我的对象需要符合NSSecureCodeding或Codable。因为我想使用JSONECODER和JSONdecoder,所以我想改用Codable。在实现类声明时,Xcode让我知道CLLocationCoodinate2D不符合Codable。在网上搜索,我找

我想在地图上显示我的一个对象。因此,在类声明中,我使其符合
MKAnnotation
。符合MKAnnotation要求我添加类型为CLLocationCoordinate2D的坐标属性。但我希望以后能够将这些对象发送到Web服务器,因此我的对象需要符合NSSecureCodeding或Codable。因为我想使用JSONECODER和JSONdecoder,所以我想改用Codable。在实现类声明时,Xcode让我知道
CLLocationCoodinate2D
不符合Codable。在网上搜索,我找到了一个解决方案,通过声明一个类型包装器(我将显示代码),使类符合可编码的要求,但即使这样解决了可编码的问题,我也无法使类符合MKAnnotation。

    class SomeClass: NSObject, MKAnnotation, Codable {
var property1: String
var coordinate: CLLocationCoordinate2D //required for conformance to MKAnnotation
var image: Image //another property that would not conform to Coadable, but i could make it using a type wrapper.
}
struct Image: Codable {
let imageData: Data? 
init(with image: UIImage) {
self.imageData = image.png()
}
public func getImage()-> UIImage? {
guard let imageData = self.imageData, let image = UIImage(data: imageData) else {return nil}
return image
}

如果使用具有坐标属性的相同解决方案,则,然后我不再遵从
MKAnnotation

您需要使用
CodingKeys
枚举来排除
coordinate
属性,并添加其他属性来表示纬度和经度值实际的JSON应该是什么样子?一旦合并了编码键、自定义编码(to:)和init(from:),您就有了许多选项方法。请参见示例。您需要使用
CodingKeys
枚举来排除
coordinate
属性,并添加其他属性来表示纬度和经度值实际JSON应该是什么样子的?一旦合并了编码键、自定义编码(to:)和初始化(from:)方法,您就有了许多选项。
    class SomeClass: NSObject, MKAnnotation, Codable {
var property1: String
var coordinate: CLLocationCoordinate2D //required for conformance to MKAnnotation
var image: Image //another property that would not conform to Coadable, but i could make it using a type wrapper.
}
struct Image: Codable {
let imageData: Data? 
init(with image: UIImage) {
self.imageData = image.png()
}
public func getImage()-> UIImage? {
guard let imageData = self.imageData, let image = UIImage(data: imageData) else {return nil}
return image
}