比较 2 个不同 NSArray 对象中的值

发布于 2024-09-07 20:29:40 字数 355 浏览 1 评论 0原文

我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

想你只要分分秒秒 2024-09-14 20:29:45

我可能会使用 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.

一个人的旅程 2024-09-14 20:29:43

如果您喜欢使用集合而不是数组,则可以使用 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: and minusSet:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文