简体   繁体   English

核心数据子类ManagedObject的多对多关系addObject无法正常工作

[英]Core data subclass ManagedObject's to-many relationship addObject can't work

I have two subclass of ManagedObject as below 我有两个ManagedObject的子类,如下所示

在此处输入图片说明

which is one-to-many relationship between them. 它们之间是一对多的关系。

In order to add definition to word, I implement the func below: 为了给单词添加定义,我实现了以下功能:

func addDefinition(definition: MODefinition) {

    self.mutableSetValueForKey("definitions").addObject(definition)

}

But when I run the function, it crashed and show message below: 但是当我运行该函数时,它崩溃了并在下面显示消息:

'NSInvalidArgumentException', reason: '*** -[NSSet initWithArray:range:copyItems:]: array argument is not an NSArray'

I also try to assign word to definition like : definition.word = word 我也尝试将单词分配给定义,例如: definition.word = word

But it appear the same crash message. 但是它出现了相同的崩溃消息。

However, I can assign NSSet to word.definitons like this: 但是,我可以将NSSet分配给word.definitons,如下所示:

let definitionSet = NSSet(array: definitions)
    word.definitions = definitionSet

But this method will not let me append object... 但是这种方法不会让我追加对象...

Also, my other managedObject can use this kind of function self.mutableSetValueForKey to addObject. 另外,我的其他ManagedObject可以使用此类函数self.mutableSetValueForKey来添加对象。 So I don't know where did it go wrong. 所以我不知道哪里出了问题。

EDIT: 编辑:

I'm trying to removeObject : 我正在尝试removeObject:

func removeDefinition(definition: MODefinition) {

    let muSet = self.mutableSetValueForKey("definitions")
    muSet.removeObject(definition)

}

And It shows 它表明

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSSingleObjectSetI objectAtIndex:]: unrecognized selector sent to instance

I'd trying to implement on other objects, but only this object can't work. 我试图在其他对象上实现,但仅此对象无法工作。

What should I check for this problem, I'm really stuck now. 我应该怎么检查这个问题,我现在真的很困。

EDIT2 : 编辑2:

I add the new relationship to test if the problem is due to the relationship. 我添加了新的关系以测试问题是否归因于该关系。

在此处输入图片说明

So, I added the test relationship. 因此,我添加了测试关系。

And.... It work... 而且...

Although, I still don't know the reason, I can run normally now. 虽然,我仍然不知道原因,但是现在我可以正常运行了。

i'm also new to ios.hope this will help you. 我也是ios.hope的新手,它将为您提供帮助。

make NSManaged in Moword class. 在Moword类中设置NSManaged like: 喜欢:

@NSManaged var word: NSSet? @NSManaged var字:NSSet?

and then make NSMutableSet object 然后使NSMutableSet对象

let wordMutableSet = NSMutableSet() 让wordMutableSet = NSMutableSet()

and every time create new object of MoDefination 并每次创建MoDefination的新对象

let entity =  NSEntityDescription.entityForName("MoDeination", inManagedObjectContext: Utilities.getAppManagedContext())

let obj = MoDefination(entity: entity!, insertIntoManagedObjectContext: Utilities.getAppManagedContext())

wordMutableSet.addObject(obj) wordMutableSet.addObject(obj)

and add it now in Moword object Moword entity1 = NSEntityDescription.entityForName("Moword", inManagedObjectContext: Utilities.getAppManagedContext()) 并将其立即添加到Moword对象Moword实体1 = NSEntityDescription.entityForName(“ Moword”,inManagedObjectContext:Utilities.getAppManagedContext())中

let obj = Moword(entity: entity!, insertIntoManagedObjectContext: Utilities.getAppManagedContext())
obj?.scores = wordMutableSet

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM