问题
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;
回答1:
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
let entity = NSEntityDescription.entityForName("XMPPGroupCoreDataStorageObject", inManagedObjectContext: moc!)
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!
回答2:
Well, you are trying to send the message to an object that cannot respond to it.
Look at your exception log:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString managedObjectContext]: unrecognized selector sent to instance 0x7fdf8c545e50'***
managedObjectContext
message has been sent to an instance of NSString
class.
Of course, it cannot respond to it and you are getting the crash.
来源:https://stackoverflow.com/questions/41038176/how-to-save-and-fetch-in-xmppgroupcoredatastorageobject