首页 > 解决方案 > [MyClass encodeWithCoder:]:发送到实例的无法识别的选择器

问题描述

我正在尝试将我的 API 响应保存在我的核心数据实体中。我有一些具有自定义类的属性。这些自定义类来自第三方库,我无法进行更改。这是我的实体,其中包含自定义类属性, 在此处输入图像描述

当我尝试像这样在我的实体中保存响应时,

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 let context = appDelegate?.persistentContainer.viewContext let entity = NSEntityDescription.entity(forEntityName: "SingleChatCoreData", in: context!)

    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
}

现在,当我再次尝试保存响应时,它在同一点崩溃并出现同样的错误,我被这个问题困扰了一个多星期,请任何人都可以让我知道为什么这个问题是我正在做的错误?

标签: swiftcore-data

解决方案


推荐阅读