NSManagedObjects中的可变集? [英] Mutable sets in NSManagedObjects?
问题描述
自动生成的 NSManagedObject
属性的标准声明声明了许多关系,如下所示:
The standard declaration for an autogenerated NSManagedObject
property declares to-many relationships like this:
@property (nonatomic, retain) NSSet *somethings;
...
@dynamic somethings;
将声明更改为 NSMutableSet
? CoreData组件是否仍能正常运行?
Would it be safe to change that declaration to an NSMutableSet
? Would the CoreData component still function correctly?
根据 @dynamic
判断,probaby不关心使用 NSSet
子类,而不是 NSSet
,但我不想重写大量代码,
Judging by the @dynamic
, it probaby doesn't care if I use an NSSet
subclass rather than an NSSet
, but I don't want to rewrite a lot of code only to find out it doesn't work.
推荐答案
根据Apple的核心数据编程指南,声明为NSSet。
According to Apple's Core Data Programming Guide, this should always be declared as a NSSet.
如果你想要一个可变代理(基本上是核心数据集的可变版本,工作原理完全一样),那么你将使用函数 mutableSetValueForKey
像这样:
If you want a mutable proxy (which is basically the mutable version of the core data set and works exactly the same) then you would use the function mutableSetValueForKey
like this:
// myManagedObject is the managed object that has the somethings property.
NSMutableSet *mutableSomethings = [myManagedObject mutableSetValueForKey:@"somethings"];
mutableSomethings
NSMutableSet和 somethings
将被更新,KVO方法将被适当地调用。
mutableSomethings
can then be modified as a standard NSMutableSet andsomethings
will be updated and KVO methods will be appropriately called.
注意,已经在核心数据生成的访问器中提供了可变集(例如 addSomethingsObject
和 removeSomethingsObject
),因此在许多情况下,无需使用代理。
Note however, that many features of the mutable set (such as addSomethingsObject
and removeSomethingsObject
) are already provided in the core data generated accessors so in many cases you don't need to use the proxy.
这篇关于NSManagedObjects中的可变集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!