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]];
        }
    }
1
  • Your code doesn't seem to be doing what your description says. If all three strings are present in unique, you're going to add the indexes of all three to uniqueKind. Do you mean that you don't want more than one of each string in unique? Commented Aug 5, 2013 at 19:35

3 Answers 3

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 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:

NSUInteger i = [unique indexOfObject:obj];
if ( i != NSNotFound ) {
    [uniqueKind addObject:[NSNumber numberWithInteger:i]];
}
Sign up to request clarification or add additional context in comments.

1 Comment

@JoshCaswell Bad copy paste. Thanks for catching it. I was demonstrating that you don't need to do the containsObject: call, as indexOfObject: will tell you if it's in the array.
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.

Comments

0

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

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

instead.

(edited )

1 Comment

Oops - my bad - try converting it to an NSNumber with numberWithInt: i

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.