比较 2 个不同 NSArray 对象中的值
我有 2 个 NSArray 保存值...
例如 NSArray 1 中包含值 1 2 4
,NSArray 2 中包含值 1 2 4 5 6。
我如何编写代码来比较这两个数组以获得以下信息...
计算相同的值(在本例中为 3)并计算不相同的值(在本例中为 2)。
我只是像这样填充数组: NSString *s = @"1,2,4"; NSArray *numbers = [s ComponentsSeparatedByString:@","];
其中 *s 实际上是从 UITextField 获取文本。如果排序在比较中很重要,您可以向我展示用于排序的代码,以确保用户不会按顺序排列数字吗?
I have 2 NSArray's that are holding values...
For example NSArray 1 has values 1 2 4 in it
and NSArray 2 has values 1 2 4 5 6 in it.
How can I write code to compare these 2 arrays to get the following information...
Count the values that are the same (so in this case 3) and count the values that are not the same (in this case 2).
I am simply populating the arrays like this:
NSString *s = @"1,2,4";
NSArray *numbers = [s componentsSeparatedByString:@","];
where *s is actually getting the text from a UITextField. If sorting mattering in comparing can you show me code to sort to make sure the user doesnt put the numbers in order?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我可能会使用 NSArray 类的以下方法:
enumerateObjectsUsingBlock。
并使用以下方法对另一个数组中的成员资格进行块测试:
indexOfObjectIdenticalTo。
如果您不清楚,请告诉我。
I would probably use the following method of the NSArray class:
enumerateObjectsUsingBlock.
and code the block testing for membership in the other array with the method:
indexOfObjectIdenticalTo.
If this isn't clear to you let me know.
如果您喜欢使用集合而不是数组,则可以使用 NSMutableSet 而不是 NSArray。 NSMutableSet 有很好的方法,例如
intersectSet:
和minusSet:
If you are fine with sets instead of arrays, you can use NSMutableSet instead of NSArray. NSMutableSet has nice methods like
intersectSet:
andminusSet: