检查NSArray中对象的重复属性值 [英] Check duplicate property values of objects in NSArray

查看:292
本文介绍了检查NSArray中对象的重复属性值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 NSArray 包含 size 属性的对象。



我如何检查 NSArray 是否有两个具有相同值的对象 size

我可以这样做:

  int i = 0; 
for(id item1 in myArray){
NSDecimalNumber * size1 = [item1 size];

for(id item2 in myArray){
NSDecimalNumber * size2 = [item2 size];

if([size1 isEqual:size2]){
i ++;
}
}
}

if(i> [myArray count]){
NSLog(@Duplicate Sizes Exist);
}

还是有更简单的方法?

解决方案

尝试此代码:

  NSSet * myset = [NSSet setWithArray:[myarray valueForKey:@size]]; 
int duplicatesCount = [myarray count] - [myset count];

size


I have an NSArray containing objects with a size property.

How can I check if the NSArray has two objects with the same value for size?

Can I do something like:

int i = 0;
for (id item1 in myArray) {
    NSDecimalNumber *size1 = [item1 size];

    for (id item2 in myArray) {
        NSDecimalNumber *size2 = [item2 size];

        if ([size1 isEqual:size2]) {
            i ++;
        }
    }
}

if (i > [myArray count]) {
    NSLog(@"Duplicate Sizes Exist");
}

Or is there an easier way?

解决方案

Try this code:

NSSet *myset = [NSSet setWithArray:[myarray valueForKey:@"size"]];
int duplicatesCount = [myarray count] - [myset count];

size here is the object property.

这篇关于检查NSArray中对象的重复属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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