gpt4 book ai didi

swift - 无法通过 API 将自定义对象保存到核心数据

转载 作者:行者123 更新时间:2023-11-30 11:45:46 24 4
gpt4 key购买 nike

我从在线 API 接收 JSON 数据,并尝试将数据放入自定义 TVShow 对象中,然后使用 CoreData 保存这些对象。我可以正确打印从 JSON 对象获取的值,但是当我尝试创建 TVShow 对象并使用 CoreData 保存它们时,出现错误。有什么建议吗?

Alamofire.request("https://api.tvmaze.com/shows?page=0").responseJSON { 
(responseData) -> Void in
if((responseData.result.value) != nil) {
let swiftyJsonVar = JSON(responseData.result.value!)
//print(swiftyJsonVar)
for item in swiftyJsonVar.array! {
print(item["name"].stringValue)
print(item["genres"].arrayValue)
print(Int32(item["id"].intValue))
print(item["type"].stringValue)
print(item["language"].stringValue)
print(item["summary"].stringValue)


if CoreDataHandler.saveObject(
id:Int32(item["id"].intValue),
name:item["name"].stringValue,
type:item["type"].stringValue,
language:item["language"].stringValue,
summary:item["summary"].stringValue,
genres:item["genres"].arrayValue) {
self.tvshows = CoreDataHandler.fetchObject()
}
}

}
}

我收到此错误:

[_SwiftValue encodeWithCoder:]: unrecognized selector sent to instance 0x6080000a8160

2018-02-15 10:42:38.448476-0800 TV Core Data[81985:5602062] *** -[NSKeyedArchiver dealloc]: warning: NSKeyedArchiver deallocated without having had -finishEncoding called on it.

2018-02-15 10:42:38.458710-0800 TV Core Data[81985:5602062] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[_SwiftValue encodeWithCoder:]: unrecognized selector sent to instance

但是,当我使用 for 循环手动创建对象时,这行代码可以工作

for item in 1...10 {

if CoreDataHandler.saveObject(id: Int32(item), name: "tim\(item)", type: "drama\(item)", language: "english", summary: "\(item) time running out", genres: ["suspense","drama"]) {
self.tvshows = CoreDataHandler.fetchObject()

}

enter image description here

最佳答案

缺少 NSCoding 协议(protocol)的实现。

NSKeyedArchiver encodes (saves) and decodes (retrieves) any NSCoding compliant classes you want persisted. While NSKeyedArchiver is not as robust as Core Data (it’s slower and manual), it accomplishes the desired job of persisting data and can be less complex than Core Data.

NSCoding is a protocol that requires two methods — required init(coder decoder: NSCoder) and encode(with coder: NSCoder). If I create a class that conforms to NSObject AND NSCoder, then my class can be serialized (translated from its current data structure into a format that can be stored as bytes) and deserialized (extracted from bytes into a data structure) into data that can be saved to a user’s disk.

示例代码:

class Example : NSObject, NSCoding {

struct Keys {
static let Name = "name"
}

var name: String

init(name: String) {
self.name = name
}

required init?(coder aDecoder: NSCoder) {
guard let decodedName = aDecoder.decodeObject(forKey: Keys.Name) as? String else {
return nil
}
self.name = decodedName
}

func encode(with aCoder: NSCoder) {
aCoder.encode(self.name, forKey: Keys.Name)
}
}

关于swift - 无法通过 API 将自定义对象保存到核心数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48844383/

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