所以,我正在努力使我的模型类符合 NSManagedObject (对于CoreData持久性)和 Codable (对于JSON序列化/反序列化)。现在我的JSON解码器在反序列化之前得到了大约一半的错误
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Land setGun:]: unrecognized selector sent to instance 0x60400028f780'
我一直在遵循指南,以确保符合这里发现的两种模型协议How to use swift 4 Codable in Core Data?并且我意识到错误被抛出,因为(如果错误是可信的)Land类没有' setGun'方法。令我难以置信的是
A)当我解码Land类JSON时它没有发生错误(它处理没有问题),要击中的最后一个断点实际上是在Sea类的JSON解码器中。
B)Land类有一个'setGun'方法(我在类中声明了一个占位符方法,以确保它与自动生成的CoreData代码有关)。
我的Land类代码如下:
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 dataModel也是屏幕上限:
最后,如果所有这些都不足以诊断出现了什么问题,那么您可以自由克隆存储库并从以下公共github存储库本地运行它:https://github.com/jamesjmtaylor/weg-ios
答案 0 :(得分:1)
您在Air.swift
文件中的问题
我真的不知道为什么不解码Gun at Air文件解码器
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)