Swift Core数据存储自定义类返回零

发布于 2025-01-27 04:58:57 字数 2678 浏览 1 评论 0原文

我想用自定义对象存储到核心数据的数组。我想用自定义类存储两个阵列。第一个将是检查点,如阵列< [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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文