从 NSSet 获取对象
如果您无法从 NSSet 中使用 objectAtIndex: 获取对象,那么如何检索对象?
If you can't get an object with objectAtIndex: from an NSSet then how do you retrieve objects?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
一个集合有多个用例。您可以枚举(例如使用
enumerateObjectsUsingBlock
或 NSFastEnumeration),调用containsObject
用于测试成员资格,使用anyObject
获取成员(不是随机的),或使用所有对象
。当您不想重复、不关心顺序并且想要快速进行成员资格测试时,集合是合适的。
There are several use cases for a set. You could enumerate through (e.g. with
enumerateObjectsUsingBlock
or NSFastEnumeration), callcontainsObject
to test for membership, useanyObject
to get a member (not random), or convert it to an array (in no particular order) withallObjects
.A set is appropriate when you don't want duplicates, don't care about order, and want fast membership testing.
NSSet 没有方法 objectAtIndex:
尝试调用 allObjects ,它返回所有对象的 NSArray 。
NSSet doesn't have a method objectAtIndex:
Try calling allObjects which returns an NSArray of all the objects.
如果您有某种唯一标识符来选择所需的对象,则可以使用filteredSetUsingPredicate。
首先创建谓词(假设对象中的唯一 id 称为“标识符”并且它是一个 NSString):
然后使用谓词选择对象:
it is possible to use filteredSetUsingPredicate if you have some kind of unique identifier to select the object you need.
First create the predicate (assuming your unique id in the object is called "identifier" and it is an NSString):
And then choose the object using the predicate:
NSArray *myArray = [myNSSet allObjects];
将 NSUInteger 替换为所需对象的索引。
NSArray *myArray = [myNSSet allObjects];
replace NSUInteger with the index of your desired object.
对于 Swift3 和iOS10:
For Swift3 & iOS10 :
NSSet 使用 isEqual: 方法(您放入该集合中的对象必须重写该方法,此外,还必须覆盖哈希方法)来确定对象是否在其中。
因此,例如,如果您有一个通过 id 值定义其唯一性的数据模型(假设该属性是:
那么您将实现 isEqual: as
并且您可以实现 hash:
然后,您可以获得具有该 ID 的对象(如以及检查它是否在 NSSet 中):
但是,是的,从 NSSet 中获取某些内容的唯一方法是首先考虑定义其唯一性的方法,
我还没有测试更快的方法,但我避免使用。枚举,因为这可能是线性的,而使用 member: 方法会快得多,这是更喜欢使用 NSSet 而不是 NSArray 的原因之一。
NSSet uses the method isEqual: (which the objects you put into that set must override, in addition, the hash method) to determine if an object is inside of it.
So, for example if you have a data model that defines its uniqueness by an id value (say the property is:
then you'd implement isEqual: as
and you could implement hash:
Then, you can get an object with that ID (as well as check for whether it's in the NSSet) with:
But yeah, the only way to get something out of a NSSet is by considering that which defines its uniqueness in the first place.
I haven't tested what's faster, but I avoid using enumeration because that might be linear whereas using the member: method would be much faster. That's one of the reasons to prefer the use of NSSet instead of NSArray.
大多数时候,您并不关心从一组中获取一个特定的对象。您关心的是测试集合是否包含对象。这就是集合的好处。当您想查看某个对象是否在集合中时,集合比数组快得多。
如果您不关心获得哪个对象,请使用
-anyObject
它只为您提供集合中的一个对象,就像将手放入袋子中并抓起某物一样。如果您关心获得的对象,请使用
-member
来返回该对象,如果它不在集合中,则使用 nil 。在调用该对象之前,您需要已经拥有该对象。您可以在 Xcode 中运行以下一些代码来了解更多信息
Most of the time you don't care about getting one particular object from a set. You care about testing to see if a set contains an object. That's what sets are good for. When you want to see if an object is in a collection sets are much faster than arrays.
If you don't care about which object you get, use
-anyObject
which just gives you one object from the set, like putting your hand in a bag and grabbing something.If you care about what object you get, use
-member
which gives you back the object, or nil if it's not in the set. You need to already have the object before you call it.Here's some code you can run in Xcode to understand more