xcode 为什么苹果在 CoreData 中使用 NSOrderedSet 而不是 NSArray 来实现有序的多值关系?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13031704/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why does apple use NSOrderedSet instead of NSArray for ordered mutli value relationship in CoreData?
提问by Anonymous White
In core data if we have multi value relationship, such as when a business can have several keywords, in the subclass NSManagedObject generator, Apple will use NSOrderedSet instead of NSArray.
在核心数据中如果我们有多值关系,比如当一个业务可以有多个关键字时,在子类NSManagedObject生成器中,Apple会使用NSOrderedSet而不是NSArray。
They are both almost equal except that checking ownership is slightly faster in NSOrderedSet.
除了在 NSOrderedSet 中检查所有权稍快之外,它们几乎相等。
Any reason why Apple doesn't use the far more familiar and famous NSArray?
苹果为什么不使用更为熟悉和著名的 NSArray 的任何原因?
回答by Martin R
My guess would be: Because NSOrderedSet
manages a collection of distinctelements, similar to NSSet
for unordered relationships.
我的猜测是:因为NSOrderedSet
管理不同元素的集合,类似于NSSet
无序关系。
With NSArray
you could create an object which is related to another object more than once.
随着NSArray
你可以创建这是关系到另一个对象不止一次的对象。
回答by Rob Napier
From the docs:
从文档:
You can use ordered sets as an alternative to arrays when the order of elements is important and performance in testing whether an object is contained in the set is a consideration— testing for membership of an array is slower than testing for membership of a set.
当元素的顺序很重要并且需要考虑测试对象是否包含在集合中的性能时,您可以使用有序集合作为数组的替代方案——测试数组的成员资格比测试集合的成员资格慢。