Swift Core数据存储自定义类返回零
我想用自定义对象存储到核心数据的数组。我想用自定义类存储两个阵列。第一个将是检查点,如阵列< [checkpoint]>第二个将作为数组轨迹。这是数据模型:数据模型
import Foundation
import CoreData
public class Checkpoint: NSObject, NSCoding {
public static var supportsSecureCoding = true
public var longitude:Double?
public var latitude:Double?
public var instruction:String?
public var tts:Bool?
public var len_track:Int?
init(longitude: Double, latitude: Double, instruction: String, tts: Bool, len_track: Int) {
self.longitude = longitude
self.latitude = latitude
self.instruction = instruction
self.tts = tts
self.len_track = len_track
}
public func encode(with coder: NSCoder) {
coder.encode(longitude, forKey: "longitude")
coder.encode(latitude, forKey: "latitude")
coder.encode(instruction, forKey: "instruction")
coder.encode(tts, forKey: "tts")
coder.encode(len_track, forKey: "len_track")
}
public required init?(coder: NSCoder) {
guard let longitude = coder.decodeObject(forKey: "longitude") as? Double,
let latitude = coder.decodeObject(forKey: "latitude") as? Double,
let instruction = coder.decodeObject(forKey: "instruction") as? String,
let tts = coder.decodeObject(forKey: "tts") as? Bool,
let len_track = coder.decodeObject(forKey: "len_track") as? Int else {
return nil
}
self.longitude = longitude
self.latitude = latitude
self.instruction = instruction
self.tts = tts
self.len_track = len_track
}
}
public class Coordinates: NSObject, NSCoding {
public static var supportsSecureCoding = true
var latitude: Float?
var longitude: Float?
init(longitude: Float, latitude: Float) {
self.longitude = longitude
self.latitude = latitude
}
public func encode(with coder: NSCoder) {
coder.encode(longitude, forKey: "longitude")
coder.encode(latitude, forKey: "latitude")
}
public required init?(coder: NSCoder) {
guard let longitude = coder.decodeObject(forKey: "longitude") as? Float,
let latitude = coder.decodeObject(forKey: "latitude") as? Float else {
return nil
}
self.longitude = longitude
self.latitude = latitude
}
}
与两个可转换实体相距甚远,一切都可以正常存储。但是,当我添加检查点并跟踪到路由时,我会收到以下错误:
Thread 1: "*** -decodeObjectForKey: cannot be sent to an abstract object of class NSCoder: Create a concrete instance!"
I want to store Arrays with custom Objects to Core Data. I want to store two arrays with custom Classes. The first would be checkpoints as Array<[Checkpoint]> and the second one would be track as Array . Here's the Data Model: Data Model
import Foundation
import CoreData
public class Checkpoint: NSObject, NSCoding {
public static var supportsSecureCoding = true
public var longitude:Double?
public var latitude:Double?
public var instruction:String?
public var tts:Bool?
public var len_track:Int?
init(longitude: Double, latitude: Double, instruction: String, tts: Bool, len_track: Int) {
self.longitude = longitude
self.latitude = latitude
self.instruction = instruction
self.tts = tts
self.len_track = len_track
}
public func encode(with coder: NSCoder) {
coder.encode(longitude, forKey: "longitude")
coder.encode(latitude, forKey: "latitude")
coder.encode(instruction, forKey: "instruction")
coder.encode(tts, forKey: "tts")
coder.encode(len_track, forKey: "len_track")
}
public required init?(coder: NSCoder) {
guard let longitude = coder.decodeObject(forKey: "longitude") as? Double,
let latitude = coder.decodeObject(forKey: "latitude") as? Double,
let instruction = coder.decodeObject(forKey: "instruction") as? String,
let tts = coder.decodeObject(forKey: "tts") as? Bool,
let len_track = coder.decodeObject(forKey: "len_track") as? Int else {
return nil
}
self.longitude = longitude
self.latitude = latitude
self.instruction = instruction
self.tts = tts
self.len_track = len_track
}
}
public class Coordinates: NSObject, NSCoding {
public static var supportsSecureCoding = true
var latitude: Float?
var longitude: Float?
init(longitude: Float, latitude: Float) {
self.longitude = longitude
self.latitude = latitude
}
public func encode(with coder: NSCoder) {
coder.encode(longitude, forKey: "longitude")
coder.encode(latitude, forKey: "latitude")
}
public required init?(coder: NSCoder) {
guard let longitude = coder.decodeObject(forKey: "longitude") as? Float,
let latitude = coder.decodeObject(forKey: "latitude") as? Float else {
return nil
}
self.longitude = longitude
self.latitude = latitude
}
}
Apart from the two Transformable Entities, everything can be stored normally. But when I add checkpoints and track to a route, I get the following error:
Thread 1: "*** -decodeObjectForKey: cannot be sent to an abstract object of class NSCoder: Create a concrete instance!"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论