本文介绍了如何保存和获取xmppGroupCoreDataStorageObject?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 xmpp-messenger-ios ,我创建了组并设置其配置并将用户添加到其中,然后我想将组添加到<$



如何将组数据保存到xmppGroupCoreDataStorageObject中:c $ c> xmppGroupCoreDataStorageObject 存储将其列入OpenChatViewController表视图。

  public class func addUserInCoreData(jid:String,users:Set< NSObject>){

let moc = OneRoster.sharedInstance.managedObjectContext_roster()as NSManagedObjectContext?
let entity = NSEntityDescription.entityForName(XMPPGroupCoreDataStorageObject,inManagedObjectContext:moc!)
let person = NSManagedObject(entity:entity !, insertIntoManagedObjectContext:moc!)
person.setValue(jid,forKey :name)
person.setValue(users,forKey:users)//代码在这里。
print(moc.debugDescription)

do {
try moc?.save()
let sucess = XMPPGroupCoreDataStorageObject.insertGroupName(jid,inManagedObjectContext:moc)
print(sucess)
}
catch let error {
print(error)
}

}



遭遇异常

  ***由于未捕获异常而终止应用程序'NSInvalidArgumentException',reason:' -  [__ NSCFString managedObjectContext]:无法识别的选择器发送到实例0x7fdf8c545e50'*** 
pre>

这里是框架实现:

  @property(nonatomic,strong)NSString * name; 
@property(nonatomic,strong)NSSet * users;


解决方案

首先,对于 users 行的setValue,一个猜测是您的 users 集不包含类型 XMPPUserCoreDataStorageObject (但它应该)。我认为您的方法签名应该是:

  public class func addUserInCoreData(jid:String,users:Set< XMPPUserCoreDataStorageObject>)

检查调用此函数的位置,可能是传递一组非 XMPPUserCoreDataStorageObject 对象,但一些不同的类型,这是错误的。



但是更重要的是: strong>

  let entity = XMPPGroupCoreDataStorageObject.insertGroupName(jid,inManagedObjectContext:moc)

尝试moc?.save()之后。此方法返回一个应该配置然后保存的 XMPPGroupCoreDataStorageObject 对象,而不是指示成功保存的bool。



例如,查看本教程(注意保存到核心数据部分中的 saveName 方法):
https://www.raywenderlich.com/115695/getting-started-with-core-data-tutorial a>



祝你好运!


Using xmpp-messenger-ios, I have created the group and set its configuration and adds the users into it, then I wants to add the group into the xmppGroupCoreDataStorageObject storage to list it into the OpenChatViewController table view.

How I am saving the Group Data into xmppGroupCoreDataStorageObject:

public class func addUserInCoreData(jid:String, users: Set<NSObject>) {

        let moc = OneRoster.sharedInstance.managedObjectContext_roster() as NSManagedObjectContext?
        let entity = NSEntityDescription.entityForName("XMPPGroupCoreDataStorageObject", inManagedObjectContext: moc!)
        let person = NSManagedObject(entity: entity!, insertIntoManagedObjectContext: moc!)
        person.setValue(jid, forKey: "name")
        person.setValue(users, forKey: "users") // Code breaks here.
        print(moc.debugDescription)

        do{
            try moc?.save()
            let sucess = XMPPGroupCoreDataStorageObject.insertGroupName(jid, inManagedObjectContext: moc)
            print(sucess)
        }
        catch let error{
            print(error)
        }

    }

Encountering the Exception:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString managedObjectContext]: unrecognized selector sent to instance 0x7fdf8c545e50'***

Here is the Framework Implementation:

@property (nonatomic, strong) NSString * name;
@property (nonatomic, strong) NSSet* users;
解决方案

First of all, since you are saying that it crashes on the setValue for users line, one guess would be that your users set doesn't really contain objects of type XMPPUserCoreDataStorageObject (but it's supposed to). I think your method signature should be:

public class func addUserInCoreData(jid: String, users: Set<XMPPUserCoreDataStorageObject>)

Check the place where you call this function, it might be the case that you are passing a set of not XMPPUserCoreDataStorageObject objects, but some different type, which is wrong.

But probably even more importantly:

let entity = XMPPGroupCoreDataStorageObject.insertGroupName(jid, inManagedObjectContext: moc)

should be called instead of

and not after try moc?.save(). This method returns a XMPPGroupCoreDataStorageObject object that should be configured and then saved, not a bool indicating a successful save.

Check out, for example, this tutorial (note the saveName method in the Saving to Core Data part):https://www.raywenderlich.com/115695/getting-started-with-core-data-tutorial

Good luck!

这篇关于如何保存和获取xmppGroupCoreDataStorageObject?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 11:17