How to add element to NSOrderedSet for Core Data?

折月煮酒 提交于 2019-12-02 19:42:44

Swift:

let mutableItems = items.mutableCopy() as! NSMutableOrderedSet
mutableItems.addObject(anItem)
items = mutableItems.copy() as! NSOrderedSet

Objective-C:

NSMutableOrderedSet *mutableItems = (NSMutableOrderedSet *)items.mutableCopy;
[mutableItems addObject:anItem];
items = (NSOrderedSet *)mutableItems.copy;
Bagusflyer

The problem solved. There are two problems:

  1. Can't insert/add an existing item
  2. I have a stupid name conflict. So the suggestion in this post is good.

Thanks.

Another way of doing this is with the mutableOrderedSetValueForKey function in NSKeyValueCoding. You could say

owningObject.mutableOrderedSetValueForKey("items").addObject(anItem)

Unfortunately, this doesn't get you the compile-time checking of "items."

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!