我正在尝试将API响应保存在我的核心数据实体中。我有一些具有自定义类的属性。这些自定义类来自第三方库,我无法进行更改。这是我的实体,其中包含自定义类属性, enter image description here
当我尝试将回复保存到这样的实体中时,
let appDelegate = UIApplication.shared.delegate as? AppDelegate
let context = appDelegate?.persistentContainer.viewContext
let entity = NSEntityDescription.entity(forEntityName: "SingleChatCoreData", in: context!)
let user = NSManagedObject(entity: entity!, insertInto: context)
for items in dateWiseSortedSingleRooms
{
user.setValue(items.name, forKey: "name")
user.setValue(items.actualNameFor_1_2_1_chat, forKey: "actualNameFor_1_2_1_chat")
user.setValue(items.isGroup, forKey: "isGroup")
user.setValue(items.lastMsgRead, forKey: "lastMsgRead")
user.setValue(items.lastMsgTimeActual, forKey: "lastMsgTimeActual")
user.setValue(items.lastMessage, forKey: "lastMessage")
user.setValue(items.lastMsgTime, forKey: "lastMsgTime")
user.setValue(items.profilePic, forKey: "profilePic")
user.setValue(items.roomSID, forKey: "roomSID")
user.setValue(items.isNewGroup, forKey: "isNewGroup")
user.setValue(items.unReadMsgsCount, forKey: "unReadMsgsCount")
user.setValue(items.unReadMsgsCount, forKey: "unReadMsgsCount")
user.setValue(items.members, forKey: "members")
user.setValue(items.messages, forKey: "messages")
user.setValue(items.twChannelObj, forKey: "twChannelObj")
}
do {
try context?.save()
print("Saved successfully.")
} catch {
print("Fail to save")
}
应用程序崩溃,并在控制台中显示错误消息
error: SQLCore dispatchRequest: exception handling request: <NSSQLSaveChangesRequestContext: 0x28389ff20> , -[TCHMember encodeWithCoder:]: unrecognized selector sent to instance 0x2809f75c0 with userInfo of (null)
我已经搜索了错误,但我得出的结果是应该使用NSCoding协议。然后我在NSManagedObjecClass中为这样的实体使用了NSCoding协议,
import CoreData
导入TwilioChatClient
@objc(SingleChatCoreData) 公共类SingleChatCoreData:NSManagedObject,NSCoding {
public required convenience init?(coder aDecoder: NSCoder) {
let name = aDecoder.decodeObject(forKey: "name") as? String
let roomSID = aDecoder.decodeObject(forKey: "roomSID") as? String
let isGroup = aDecoder.decodeBool(forKey: "isGroup")
let lastMessage = aDecoder.decodeObject(forKey: "lastMessage") as? String
let lastMsgTime = aDecoder.decodeObject(forKey: "lastMsgTime") as? String
let lastMsgTimeActual = aDecoder.decodeObject(forKey: "lastMsgTimeActual") as? String
let profilePic = aDecoder.decodeObject(forKey: "profilePic") as? String
let lastMsgRead = aDecoder.decodeBool(forKey: "lastMsgRead")
let unReadMsgsCount = aDecoder.decodeInt32(forKey: "unReadMsgsCount")
let actualNameFor_1_2_1_chat = aDecoder.decodeObject(forKey: "actualNameFor_1_2_1_chat") as? String
let isNewGroup = aDecoder.decodeBool(forKey: "isNewGroup")
let twChannelObj = aDecoder.decodeObject(forKey: "twChannelObj") as? TCHChannel
let members = aDecoder.decodeObject(forKey: "members") as? [TCHMember]
let messages = aDecoder.decodeObject(forKey: "messages") as? [TCHMessage]
self.init(name:name!,roomSID:roomSID!,isGroup:isGroup,lastMessage:lastMessage!,lastMsgTime:lastMsgTime!,lastMsgTimeActual:lastMsgTimeActual!,profilePic:profilePic!,lastMsgRead:lastMsgRead,unReadMsgsCount:Int16(unReadMsgsCount),actualNameFor_1_2_1_chat:actualNameFor_1_2_1_chat!,isNewGroup:isNewGroup,twChannelObj:twChannelObj!,members:members!,messages:messages!)
}
public func encode(with encoder: NSCoder) {
encoder.encode(name, forKey: "name")
encoder.encode(roomSID, forKey: "roomSID")
encoder.encode(isGroup, forKey: "isGroup")
encoder.encode(lastMessage, forKey: "lastMessage")
encoder.encode(lastMsgTime, forKey: "lastMsgTime")
encoder.encode(lastMsgTimeActual, forKey: "lastMsgTimeActual")
encoder.encode(profilePic, forKey: "profilePic")
encoder.encode(lastMsgRead, forKey: "lastMsgRead")
encoder.encode(unReadMsgsCount, forKey: "unReadMsgsCount")
encoder.encode(actualNameFor_1_2_1_chat, forKey: "actualNameFor_1_2_1_chat")
encoder.encode(isNewGroup, forKey: "isNewGroup")
encoder.encode(twChannelObj, forKey: "twChannelObj")
encoder.encode(members, forKey: "members")
encoder.encode(messages, forKey: "messages")
}
init(name:String,roomSID:String,isGroup:Bool,lastMessage:String,lastMsgTime:String,lastMsgTimeActual:String,profilePic:String,lastMsgRead:Bool,unReadMsgsCount:Int16,actualNameFor_1_2_1_chat:String,isNewGroup:Bool,twChannelObj:TCHChannel?,members:[TCHMember],messages:[TCHMessage])
{
让appDelegate = UIApplication.shared.delegate为? AppDelegate
让context = appDelegate?.persistentContainer.viewContext
让实体= NSEntityDescription.entity(forEntityName:“ SingleChatCoreData”,在:上下文中!)
super.init(entity: entity!, insertInto: context)
self.name = name
self.roomSID = roomSID
self.isGroup = isGroup
self.lastMessage = lastMessage
self.lastMsgTime = lastMsgTime
self.lastMsgTimeActual = lastMsgTimeActual
self.profilePic = profilePic
self.lastMsgRead = lastMsgRead
self.unReadMsgsCount = unReadMsgsCount
self.actualNameFor_1_2_1_chat = actualNameFor_1_2_1_chat
self.isNewGroup = isNewGroup
self.twChannelObj = twChannelObj
self.members = members
self.messages = messages
}
} 现在,再次,当我尝试保存响应时,它在同一点上崩溃并出现相同的错误,我坚持这个问题一个多星期了,请问任何人都可以让我知道为什么这个问题是什么原因,那么我在做什么错误?