gpt4 book ai didi

swift - Codable CoreData 模型因 `Unrecognized selector sent to instance` 而崩溃

转载 作者:行者123 更新时间:2023-11-28 05:58:47 26 4
gpt4 key购买 nike

因此,我正在努力使我的模型类符合 NSManagedObject(用于 CoreData 持久性)和 Codable(用于 JSON 序列化/反序列化)。现在我的 JSON 解码器在反序列化过程中大约进行了一半,然后它因错误而崩溃

由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[Land setGun:]:无法识别的选择器发送到实例 0x60400028f780”

我一直在遵循此处找到的两个模型协议(protocol)的一致性指南 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 数据模型也被截屏如下:

CoreData dataModel

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

最佳答案

你在 Air.swift 文件这一行的问题我真的不知道为什么不在Air文件解码器上解码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)

关于swift - Codable CoreData 模型因 `Unrecognized selector sent to instance` 而崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50553220/

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