0

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.

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"

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]];
        }
    }
4

3 回答 3

4

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

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

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

NSUInteger i = [unique indexOfObject:obj];
if ( i != NSNotFound ) {
    [uniqueKind addObject:[NSNumber numberWithInteger:i]];
}
于 2013-08-05T19:33:49.093 回答
2

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

于 2013-08-05T19:42:23.307 回答
0

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

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

反而。

(已编辑)

于 2013-08-05T19:33:02.593 回答