gpt4 book ai didi

swift - 不兼容对象的 NSCoding (MKMapItem)

转载 作者:行者123 更新时间:2023-11-30 12:55:32 29 4
gpt4 key购买 nike

尝试将 MKMapItem 保存为我的自定义类的一部分。

 import UIKit
import MapKit

class Place: NSObject, NSCoding {

var mapItem : MKMapItem!
var type : Category!

init(mapItem: MKMapItem, type: Category) {
self.mapItem = mapItem
self.type = type

}

// MARK: NSCoding
required init?(coder decoder: NSCoder) {

mapItem = decoder.decodeObject(forKey: "mapItem") as! MKMapItem?
type = decoder.decodeObject(forKey: "type") as! Category?
}

func encode(with coder: NSCoder) {

coder.encode(mapItem, forKey: "mapItem")
coder.encode(type, forKey: "type")
}

}

但这不起作用,因为 MKMapItem 不符合 NSCoding 标准(但编译器不会提示)。我确实了解如何对自定义类进行编码,但不知道如何对 iOS 已定义的对象进行编码。

我知道 Objective-C 对此有一个答案,但我真的很想要一个 Swift 解决方案。谢谢。

PS 我尝试对 MKMapItem 进行子类化并提供"new"初始化程序,尽管这会在其他地方进行大量代码更改。但这会导致“无法分配给属性:‘地标’是仅获取属性”。 “placemark”和“isCurrentLocation”都是仅获取的。

import UIKit
import MapKit

class NewMapItem: MKMapItem {


required init(placemark: MKPlacemark, isCurrentLocation:Bool, name: String, phoneNumber: String, url: URL, timeZone: TimeZone) {
self.placemark = placemark //compiler complains "get-only"
self.isCurrentLocation = isCurrentLocation //compiler complains "get-only"
self.name = name
self.phoneNumber = phoneNumber
self.url = url
self.timeZone = timeZone

}

// MARK: NSCoding

init?(coder decoder: NSCoder) {

placemark = (decoder.decodeObject(forKey: "placemark") as! MKPlacemark?)! //compiler complains "get-only"
isCurrentLocation = decoder.decodeBool(forKey: "isCurrentLocation") //compiler complains "get-only"
name = decoder.decodeObject(forKey: "name") as? String
phoneNumber = decoder.decodeObject(forKey: "phoneNumber") as? String
url = decoder.decodeObject(forKey: "url") as! URL?
timeZone = decoder.decodeObject(forKey: "timeZone") as! TimeZone?
}

func encode(with coder: NSCoder) {

coder.encode(placemark, forKey: "placemark")
coder.encode(isCurrentLocation, forKey: "isCurrentLocation")
coder.encode(name, forKey: "name")
coder.encode(phoneNumber, forKey: "phoneNumber")
coder.encode(url, forKey: "url")
coder.encode(timeZone, forKey: "timeZone")
}
}

最佳答案

我已经弄清楚了。

分解“func编码(带有编码器:NSCoder)”中的mapItem并在“init?(编码器解码器:NSCoder)”中再次构建它

我确实利用了带有地标的 MKMapItem 初始值设定项。

   MKMapItem(placemark: placemark!)

这是我的 worker 类(Class):

import UIKit
import MapKit

class Place: NSObject, NSCoding {

var mapItem : MKMapItem!
var type : Category!

init(mapItem: MKMapItem, type: Category) {
self.mapItem = mapItem
self.type = type
}

// MARK: NSCoding
required init?(coder decoder: NSCoder) {

//liberate the properties of mapItem and rebuild it
// let isCurrentLocation = decoder.decodeObject(forKey: "isCurrentLocation") as! Bool //don't need set already
let name = decoder.decodeObject(forKey: "name") as! String?
let phoneNumber = decoder.decodeObject(forKey: "phoneNumber") as! String?
let url = decoder.decodeObject(forKey: "url") as! URL?
let timeZone = decoder.decodeObject(forKey: "timeZone") as! TimeZone?
let placemark = decoder.decodeObject(forKey: "placemark") as! MKPlacemark?

self.mapItem = MKMapItem(placemark: placemark!)
self.mapItem.name = name
self.mapItem.url = url
self.mapItem.phoneNumber = phoneNumber
//self.mapItem.isCurrentLocation = isCurrentLocation //don't need this. Set already
self.mapItem.timeZone = timeZone

type = decoder.decodeObject(forKey: "type") as! Category?

}

func encode(with coder: NSCoder) {
let placemark = mapItem.placemark
let name = mapItem.name
let phoneNumber = mapItem.phoneNumber
let url = mapItem.url
let timeZone = mapItem.timeZone

coder.encode(name, forKey: "name")
coder.encode(phoneNumber,forKey: "phoneNumber")
coder.encode(url, forKey: "url")
coder.encode(timeZone, forKey: "timeZone")
coder.encode(type, forKey: "type")
coder.encode(placemark, forKey: "placemark")

}

}

关于swift - 不兼容对象的 NSCoding (MKMapItem),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40442008/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com