问题描述
使用 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 thexmppGroupCoreDataStorageObject
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 yourusers
set doesn't really contain objects of typeXMPPUserCoreDataStorageObject
(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 aXMPPGroupCoreDataStorageObject
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-tutorialGood luck!
这篇关于如何保存和获取xmppGroupCoreDataStorageObject?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!