如何检查NSArray是否具有另一个数组的值

时间:2021-11-27 07:40:29

I have my array unique that is my main array and my array kind. I need to check that only 1 value of kind is present in the array unique. Then if there is more than 1 value of the array kind in unique I need to unset all values but the first one used in the array.

我的数组是唯一的,这是我的主阵列和我的阵列类型。我需要检查数组中只有1个类型的值是唯一的。然后,如果唯一的数组类型的值超过1,我需要取消设置除数组中使用的第一个值之外的所有值。

The further i got to achieve this is with the following code but I can not store the indexpath of the found object to do a later comparison. xcode says "bad receiver type nsinteger"

我需要进一步实现这一点是使用以下代码,但我无法存储找到的对象的索引路径以进行稍后的比较。 xcode说“坏接收器类型nsinteger”

could anyone help me to achieve this?

谁能帮我实现这个目标?

kind = @[@"#Routine",@"#Exercise",@"#Username"];
    NSMutableArray *uniqueKind = [NSMutableArray array];
    for (NSString* obj in kind) {
        if ( [unique containsObject:obj] ) {
            NSInteger i = [unique indexOfObject:obj];
            [uniqueKind addObject: [i intValue]];
        }
    }

3 个解决方案

#1


4  

An NSInteger is like an int, so you can't send it a message ([i intValue]). Also, you can't add an NSInteger to an array without making it an NSNumber or some other object type. You can do it like this:

NSInteger就像一个int,所以你不能发送消息([i intValue])。此外,您不能将NSInteger添加到数组而不使其成为NSNumber或其他对象类型。你可以这样做:

NSInteger i = [unique indexOfObject:obj];
[uniqueKind addObject: [NSNumber numberWithInteger:i]];

Also (without understanding what you're doing) you might want to use an NSSet instead of an array. And you can combine a couple of calls:

另外(不了解你正在做什么)你可能想要使用NSSet而不是数组。你可以结合几个电话:

NSUInteger i = [unique indexOfObject:obj];
if ( i != NSNotFound ) {
    [uniqueKind addObject:[NSNumber numberWithInteger:i]];
}

#2


2  

I'm not sure if it would solve your problem, but have you considered using sets (or mutable variation) instead of arrays? They ensure uniqueness and they allow you to check for intersection/containment. See the NSSet class reference.

我不确定它是否可以解决您的问题,但您是否考虑过使用集合(或可变变量)而不是数组?它们确保了唯一性,并允许您检查交叉口/收容。请参阅NSSet类引用。

#3


0  

You have to add objects to the NSMutableArray, not the actual intValue. Try converting teh integer to a NSNumber first.

您必须将对象添加到NSMutableArray,而不是实际的intValue。尝试首先将整数转换为NSNumber。

[uniqueKind addObject: [NSNumber numberWithInt:i]];

[uniqueKind addObject:[NSNumber numberWithInt:i]];

instead.

(edited )

#1


4  

An NSInteger is like an int, so you can't send it a message ([i intValue]). Also, you can't add an NSInteger to an array without making it an NSNumber or some other object type. You can do it like this:

NSInteger就像一个int,所以你不能发送消息([i intValue])。此外,您不能将NSInteger添加到数组而不使其成为NSNumber或其他对象类型。你可以这样做:

NSInteger i = [unique indexOfObject:obj];
[uniqueKind addObject: [NSNumber numberWithInteger:i]];

Also (without understanding what you're doing) you might want to use an NSSet instead of an array. And you can combine a couple of calls:

另外(不了解你正在做什么)你可能想要使用NSSet而不是数组。你可以结合几个电话:

NSUInteger i = [unique indexOfObject:obj];
if ( i != NSNotFound ) {
    [uniqueKind addObject:[NSNumber numberWithInteger:i]];
}

#2


2  

I'm not sure if it would solve your problem, but have you considered using sets (or mutable variation) instead of arrays? They ensure uniqueness and they allow you to check for intersection/containment. See the NSSet class reference.

我不确定它是否可以解决您的问题,但您是否考虑过使用集合(或可变变量)而不是数组?它们确保了唯一性,并允许您检查交叉口/收容。请参阅NSSet类引用。

#3


0  

You have to add objects to the NSMutableArray, not the actual intValue. Try converting teh integer to a NSNumber first.

您必须将对象添加到NSMutableArray,而不是实际的intValue。尝试首先将整数转换为NSNumber。

[uniqueKind addObject: [NSNumber numberWithInt:i]];

[uniqueKind addObject:[NSNumber numberWithInt:i]];

instead.

(edited )