设置" To Many" CoreData中的关系并勾选" Ordered" box将您的属性定义为NSOrderedSet。
是否有一种有效的方法可以在特定位置插入对象或上下移动对象?
或者我是否需要使用NSOrderedSet中的数据创建NSMutabledOrderedSet,插入或移动我的对象,然后将其转换回NSOrderedSet?
或者我应该使用标准的NSSet并管理一个位置'归因于自己?
答案 0 :(得分:0)
CoreData'Create NSManagedObject Subclasses ...'将生成许多方法供您操作有序集。或者你可以对Codegen做同样的事情。
目标C
@interface MyObject (CoreDataGeneratedAccessors)
- (void)insertObject:(NSManagedObject *)value inItemsAtIndex:(NSUInteger)idx;
- (void)removeObjectFromItemsAtIndex:(NSUInteger)idx;
- (void)insertItems:(NSArray<NSManagedObject *> *)value atIndexes:(NSIndexSet *)indexes;
- (void)removeItemsAtIndexes:(NSIndexSet *)indexes;
- (void)replaceObjectInItemsAtIndex:(NSUInteger)idx withObject:(NSManagedObject *)value;
- (void)replaceItemsAtIndexes:(NSIndexSet *)indexes withItems:(NSArray<NSManagedObject *> *)values;
- (void)addItemsObject:(NSManagedObject *)value;
- (void)removeItemsObject:(NSManagedObject *)value;
- (void)addItems:(NSOrderedSet<NSManagedObject *> *)values;
- (void)removeItems:(NSOrderedSet<NSManagedObject *> *)values;
@end
夫特
// MARK: Generated accessors for items
extension MyObject {
@objc(insertObject:inItemsAtIndex:)
@NSManaged public func insertIntoItems(_ value: NSManagedObject, at idx: Int)
@objc(removeObjectFromItemsAtIndex:)
@NSManaged public func removeFromItems(at idx: Int)
@objc(insertItems:atIndexes:)
@NSManaged public func insertIntoItems(_ values: [NSManagedObject], at indexes: NSIndexSet)
@objc(removeItemsAtIndexes:)
@NSManaged public func removeFromItems(at indexes: NSIndexSet)
@objc(replaceObjectInItemsAtIndex:withObject:)
@NSManaged public func replaceItems(at idx: Int, with value: NSManagedObject)
@objc(replaceItemsAtIndexes:withItems:)
@NSManaged public func replaceItems(at indexes: NSIndexSet, with values: [NSManagedObject])
@objc(addItemsObject:)
@NSManaged public func addToItems(_ value: NSManagedObject)
@objc(removeItemsObject:)
@NSManaged public func removeFromItems(_ value: NSManagedObject)
@objc(addItems:)
@NSManaged public func addToItems(_ values: NSOrderedSet)
@objc(removeItems:)
@NSManaged public func removeFromItems(_ values: NSOrderedSet)
}
否则,只需制作一个可变副本,根据需要进行操作,然后将其设置回NSManagedObject。顺便说一句,你不需要将它转换回非可变集