1

I'm attempting to implement the containsObject but with two parameters, is this possible?

Currently I've got:

if ([ myArray containsObject:@"Object1", @"Object2"]){
    return result;
} else {
    return NO;
}

and apparently there's too many arguments. I've delved through Apple's docs but I'm yet to find anything. Any suggestions?

1
  • 1
    containsObject only takes one argument. Use the && operator. Commented Nov 24, 2014 at 18:44

3 Answers 3

2

Why not just do this?

if ([ myArray containsObject:@"Object1" ] && [ myArray containsObject:@"Object 2" ] ){
    return result;
} else {
    return NO;
}
Sign up to request clarification or add additional context in comments.

Comments

1

There is too many arguments, containsObject is for a single object. (You can read its official documentation here) To fix your problem, use the && operator and call containsObject on each object individually.

if ([myArray containsObject:@"Object1"] && [myArray containsObject@"Object2"]){
    return result;
} else {
return NO;
}

Comments

1

You will have to evaluate them individually. Example:

bool MONNSArrayContainsAllObjectsIn(NSArray* const pArray, NSArray* const pSought) {
 assert(pArray);
 assert(pSought);
 assert(0 < pSought.count);

 for (id at in pSought) {
  if (false == [pArray containsObject:at]) {
   return false;
  }
 }
 return true;
}

Then your code above becomes:

return MONNSArrayContainsAllObjectsIn(myArray, @[@"Object1", @"Object2"]);

If you are working with a known number of elements (2 in this case), then you can avoid creating the temporary array -- if you prefer to make that optimization and write out all variants you need, including parameters. Other answers detail this approach.

If you have large arrays and many comparisons to perform, NSSet may be better suited for your task.

7 Comments

At first this seemed unnecessarily complex, but it makes sense to craft a more abstract/flexible approach w/error-checking. Could you explain the purpose/benefit of const? If they're immutable NSArrays, why is const necessary?
Using varargs would also avoid creating a temporary NSArray.
@mc01 the const in this example applies to the pointer, rather than the object. const ObjC objects aren't very useful in practice. applying const to a pointer simply declares that the pointer's value may not change. this is independent of the presence/absence of const for the object. like many uses of const-introduction, it is not necessary, but an intention to aid developers (subjective). examples of operations forbidden by const pointers: ++pArray, --pArray, pArray = nil, pArray += 2, pArray = 0, pArray = pSought.
@JoshCaswell absolutely +1. however, i would recommend the approach in my answer over variadics because it is safer.
@mc01 you're welcome. pArray = someOtherObj is an example of the case the const prohibits. however, *pArray = *someOtherPointer is actually a value assignment to the referenced object. this assignment is forbidden by the compiler with ObjC types, but it is perfectly legal for C types (e.g. int*, float*, struct*). the const in this case is not information useful to the compiler -- it's useful for the programmer, where the compiler will emit an error if the pointer is reassigned (or otherwise mutated). chances are, the programmer did not intend to modify the parameter. (cont)
|

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.