Codable CoreData 模型因“发送到实例的无法识别的选择器”而崩溃

Codable CoreData models crash with `Unrecognized selector sent to instance`

所以,我正在尝试使我的模型 classes 符合 NSManagedObject(对于 CoreData 持久性)和 Codable(对于 JSON serialization/deserialization)。现在,我的 JSON 解码器在因错误

崩溃之前大约完成了反序列化的一半

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Land setGun:]: unrecognized selector sent to instance 0x60400028f780'

我一直在遵循此处找到的两个模型协议的一致性指南 并且我意识到抛出错误是因为(如果要相信错误的话)Land class没有 'setGun' 方法。让我感到困惑的是

A) 我在解码 Land class JSON 时没有发生错误(它可以毫无问题地处理),最后一个断点实际上是在 Sea class 的 JSON 解码器。

B) The Land class 确实有一个 'setGun' 方法(我在 class 中声明了一个占位符方法只是为了更加确定它是否与自动生成的 CoreData 代码)。

我的土地 class 代码如下:

import Foundation
import CoreData
@objc(Land)
class Land : NSManagedObject, Equipment {
var type = EquipmentType.LAND

enum CodingKeys: String, CodingKey {
    case id
    case name
    case desc = "description"
    case groupIconUrl
    case individualIconUrl
    case photoUrl
    case primaryWeapon
    case secondaryWeapon
    case atgm
    case armor
    case speed
    case auto
    case weight
}

required convenience init(from decoder: Decoder) throws {
    guard let context = decoder.userInfo[CodingUserInfoKey.context!] as? NSManagedObjectContext else { fatalError() }
    guard let entity = NSEntityDescription.entity(forEntityName: "Land", in: context) else { fatalError() }
    self.init(entity: entity, insertInto: context)
    let container = try! decoder.container(keyedBy: CodingKeys.self)
    self.id = try container.decode(Int64.self, forKey: .id)
    self.name = try container.decodeIfPresent(String.self, forKey: .name)
    self.desc = try container.decodeIfPresent(String.self, forKey: .desc)
    self.groupIconUrl = try container.decodeIfPresent(String.self, forKey: .groupIconUrl)
    self.individualIconUrl = try container.decodeIfPresent(String.self, forKey: .individualIconUrl)
    self.photoUrl = try container.decodeIfPresent(String.self, forKey: .photoUrl)
    self.primaryWeapon = try container.decodeIfPresent(Gun.self, forKey: .primaryWeapon)
    self.secondaryWeapon = try container.decodeIfPresent(Gun.self, forKey: .secondaryWeapon)
    self.atgm = try container.decodeIfPresent(Gun.self, forKey: .atgm)
    self.armor = try container.decode(Int64.self, forKey: .armor)
    self.speed = try container.decode(Int64.self, forKey: .speed)
    self.auto = try container.decode(Int64.self, forKey: .auto)
    self.weight = try container.decode(Int64.self, forKey: .weight)
}
public func encode(to encoder: Encoder) throws {
    var container = encoder.container(keyedBy: CodingKeys.self)
    try container.encode(id, forKey: .id)
    try container.encode(name, forKey: .name)
    try container.encode(desc, forKey: .desc)
    try container.encode(groupIconUrl, forKey: .groupIconUrl)
    try container.encode(individualIconUrl, forKey: .individualIconUrl)
    try container.encode(photoUrl, forKey: .photoUrl)
    try container.encode(primaryWeapon, forKey: .primaryWeapon)
    try container.encode(secondaryWeapon, forKey: .secondaryWeapon)
    try container.encode(atgm, forKey: .atgm)
    try container.encode(armor, forKey: .armor)
    try container.encode(speed, forKey: .speed)
    try container.encode(auto, forKey: .auto)
    try container.encode(weight, forKey: .weight)
}
public func setGun(gun: Gun){}
}

我的CoreData数据模型也被截屏如下:

最后,如果所有这些还不足以诊断出问题所在,您可以自由克隆存储库并从以下 public github 存储库本地 运行 它: https://github.com/jamesjmtaylor/weg-ios

您在 Air.swift 文件中这一行的问题 我真的不知道为什么不在Air file Decoder

解码Gun
self.gun = try container.decodeIfPresent(Gun.self, forKey: .gun)
self.agm = try container.decodeIfPresent(Gun.self, forKey: .agm)
self.aam = try container.decodeIfPresent(Gun.self, forKey: .aam)
self.asm = try container.decodeIfPresent(Gun.self, forKey: .asm)