0

How can I iterate through an array for two objects to compare them, but at the second iteration (for obj2) I want to exclude comparing the object (obj1) that already was found to match? In other words, I don´t want them to both find the same object.

for (object *obj1 in array) 
    if (obj1 == "this") //run next iteration
    for (object *obj2 in array)
    // if (obj2 == @"this");   
1
  • If you are trying to keep the elements of your array unique, look at NSMutableSet and NSMutableOrderedSet (if a strong order is also needed.) Commented Feb 8, 2013 at 6:41

1 Answer 1

1
for(object *obj1 in array) {
    for (object *obj2 in array) {
        if(obj1 == obj2) continue; //this matches if the object is same
        //do your code
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

It's worth noting that == is a pointer comparison, which will only be true for the exact same object. If you want to also match different instances of an object with the same value, use -isEqual: instead.

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.