0

How do I check if two arrays have one specific object that is not related at one common index for both arrays in Objective-C?

if ([[Array1 objectAtIndex:SameIndex] containsObject:String1] && [[Array2 objectAtIndex:SameIndex] containsObject:String2]) {

    }

When I think of it, I may have to use a loop instead of an if statement.

Any help would be greatly appreciated.

3
  • Do you know the object you want to check in two different array? Or du you want to show common objects in two arrays? Commented Dec 11, 2013 at 15:01
  • Maybe post at least pseudo code for what you're trying to do Commented Dec 11, 2013 at 15:03
  • The objects is two different strings. I'll edit the question, it may be misunderstood. Commented Dec 11, 2013 at 15:07

2 Answers 2

1

The isEqual method allows you to compare two objects. You could do something similar to:

NSArray *array1 = @[[NSNumber numberWithInteger:10],
                    [NSNumber numberWithInteger:20],
                    [NSNumber numberWithInteger:30]];

NSArray *array2 = @[[NSNumber numberWithInteger:60],
                    [NSNumber numberWithInteger:70],
                    [NSNumber numberWithInteger:80]];

NSNumber *object1 = [NSNumber numberWithInteger:20];
NSNumber *object2 = [NSNumber numberWithInteger:70];

NSUInteger sameIndex = 1;
if ([[array1 objectAtIndex:sameIndex] isEqual:object1] && [[array2 objectAtIndex:sameIndex] isEqual:object2]) {
    // Do something
    NSLog(@"Validation passed!");
}

That will compare the object stored at index sameIndex in array1 and array2 with object1 and object2 respectively.

EDIT: I changed my code snippet into a working example for you to better understand.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks! A question to this solution; Lets say that the integer value at objectAtIndex will depend on an other array: [newArray count]. So instead of making many of these if statements, i could theoretically use a loop that checks this, right? Then the code would be more effective?
By the way, i got some errors using this code: Implicit conversion of 'NSUInteger' (aka 'unsigned int') to 'id' is disallowed with ARC 144:40: Incompatible integer to pointer conversion sending 'NSUInteger' (aka 'unsigned int') to parameter of type 'id' Passing argument to parameter 'anObject' here Bad receiver type 'NSUInteger' (aka 'unsigned int')
I changed my code snippet into a working example. Running this gave me the "Validation passed!" output. Keep in mind that 'objectAtIndex' return type is 'id', meaning you must compare it with an object of some sort (NSString* is an object, NSInteger is not).
1

You can use a few method on NSArray to find the object, for example:

[Aarray1 indexOfObject:...]
[Aarray1 indexOfObjectIdenticalTo:...]
[Aarray1 indexOfObjectPassingTest:...]

After that just make sure that you have an index and if index exists you know that there is an object. You have to just slightly amended code from your question.

3 Comments

I dont have any problem with finding the object, i just have to make sure that if the arrays contains the string value (Object) i've defined and that both the string values exist at the same index in both arrays, the code in the if statement will be executed. I'm sorry if my question may have been a little confusing in the first place
You can do: if ([Aarray1 indexOfObject:@"OBJECT"] == [Aarray1 indexOfObject:@"OBJECT"]) if it returns true it means that there are the same object at the same index in both array. Is it what you want?
Only the same index. But thanks, i think that code snippet you just mentioned may fix the problem, if i just modify it a little bit. +1 :)

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.