如何比较两个同等内容的NSArray? [英] How to compare two NSArrays for equal content?

查看:233
本文介绍了如何比较两个同等内容的NSArray?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个Nsarray,其中2个数组的对象相同可能是对象的索引不同,但它应该打印两者都是相等的而不管索引是什么

I have 2 Nsarray where objects of 2 arrays are same may be indexes of the object differs, but it should print both are equal irrespective of there indexes

NSArray *arr1 = [[NSArray alloc]initWithObjects:@"aa", @"bb", @"1", @"cc", nil];
NSArray *arr2 = [[NSArray alloc]initWithObjects:@"bb", @"cc", @"1", @"aa", nil];

if ([arr1 isEqualToArray:arr2])
{
    NSLog(@"Equal");
}
else
{
    NSLog(@"Not equal");
}

以上代码正在打印'不等于'但它应该打印'Equal'。我该怎么做?

the above code is printing 'Not equal' but it should print 'Equal'. How can I do this?

推荐答案

这两个数组不相等。两个数组相同是它们都以相同的顺序具有相同的对象。

Those two arrays are not equal. Two arrays are equal is they both have the same objects in the same order.

如果你想比较不考虑订单那么你需要使用两个 NSSet objects。

If you want to compare with no regard to order then you need to use two NSSet objects.

NSSet *set1 = [NSSet setWithArray:arr1];
NSSet *set2 = [NSSet setWithArray:arr2];

if ([set1 isEqualToSet:set2]) {
    // equal
}

这篇关于如何比较两个同等内容的NSArray?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆