gpt4 book ai didi

ios - 保存到核心数据“ALL 或 ANY 运算符的左侧必须是 NSArray 或 NSSet

转载 作者:搜寻专家 更新时间:2023-10-31 23:09:13 25 4
gpt4 key购买 nike

当我尝试将关系保存到核心数据时,它会在标题中抛出上述错误。我基本上是在创建一个消息传递应用程序,并附加了数据模型。当尝试保存到其他关系时,这也是在做同样的事情。我可以一次保存我所有的数据模型,还是应该一次保存一个,然后在模型及其常规属性保存后返回并添加关系。 ** Data Model Visual

    static func save(sender: UserCD, message: String) -> Conversation? {

let convo: Conversation? = nil

guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else {
return convo
}

if #available(iOS 10.0, *) {

let context = appDelegate.persistentContainer.viewContext

let now = Date()

let messageEntity = NSEntityDescription.entity(forEntityName: "Message", in: context)
let newMessage = NSManagedObject(entity: messageEntity!, insertInto: context) as! Message

let conversationEntity = NSEntityDescription.entity(forEntityName: "Conversation", in: context)
let newConvo = NSManagedObject(entity: conversationEntity!, insertInto: context) as! Conversation

newMessage.setValue(UUID().uuidString, forKey: "messageId")
newMessage.setValue(message, forKey: "text")
newMessage.setValue(now, forKey: "timestamp")
newMessage.setValue(sender, forKey: "sender")

newConvo.setValue(UUID().uuidString, forKey: "conversationId")
newConvo.setValue(now, forKey: "startDate")
newConvo.setValue(now, forKey: "lastMessageDate")
newConvo.addMessage(message: newMessage)

sender.addMessage(message: newMessage)
sender.addConversation(conversation: newConvo)

do {
try newMessage.managedObjectContext?.save()
try sender.managedObjectContext?.save()
try newConvo.managedObjectContext?.save()

return newConvo

} catch {
let saveError = error as NSError
print("Failed to save conversastion: \(saveError.debugDescription)")
}
}
return convo
}

它在“newConvo.addMessage(message: newMessage)”行失败

    @NSManaged public var messages: Message?

func addMessage(message: Message) {
let messages = self.mutableSetValue(forKey: "messages")
messages.add(message)
}

最佳答案

错误信息

The left hand side for an ALL or ANY operator must be either an NSArray or an NSSet

很清楚:

  • 您声明了 messages作为一对一关系Message .

  • 方法的发送者addMessage必须是对多关系Set<Message>

关于ios - 保存到核心数据“ALL 或 ANY 运算符的左侧必须是 NSArray 或 NSSet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45517819/

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