views:

316

answers:

2

People I'd like to see if an element at index[i] is not present in a different (or current ) array. So for instance if my array looked somewhat like this

[wer, wer4 , wer5 , werp , klo ...

Then I would like to see if aExerciseRef.IDE ( which is "sdf" for instance ) does or does not exist at index[i]. I assume I'd have to use some sort of iterator ..

for( int i = 0; i < 20; i++ )
{
   if([instruct objectAtIndex:index2 + i] != [instruct containsObject:aExerciseRef.IDE] )        
   NSLog(@"object doesn't exist at this index %i, i );
   else
   NSLog(@"well does exist")
}

I know this doesn't work , it's just to elaborate what I'd like to achieve.

edit:

I'm going to try to elaborate it a little more and be more specific.

1) First of all aExerciseRef.IDE changes everytime it get's called so one time it is "ret" another time it's "werd".

2) Imagine an array being filled with aExerciseRef.IDE's then I would like to compare if the elements in this array exist in the instruct array.

So I would like to see if the element at let's say position 2 (wtrk)

[wer, wtrk, wer , sla ...

exists in the array which was being filled with the aExerciseRef.IDE's.

I hope I've been more clear this time.

A: 

Your example makes no sense at all, and does nothing to clarify the question. You are doing a comparison between an expression with type (id)

[instruct objectAtIndex:index2 + i]

and one with type BOOL

[instruct containsObject:aExerciseRef.IDE]

And if the object you are looking for is at index x in an array, then it goes without saying that containsObject on the array will return YES for that object.

If what you want to accomplish is simply what you state in the title, then it's as easy as:

if ([[anArray objectAtIndex:index] == anObject])
  NSLog (@"Got it!");
else
  NSLog (@"Don't have it.");
Felixyz
2 problems: index might be out of bounds, and you can't use == as a method call to test equality.
Dave DeLong
A: 

Sir Lord Mighty is partially correct. Yes, your comparison is wrong. No, his solution won't work.

Here's one that will:

if (index < [anArray count] && [[anArray objectAtIndex:index] isEqual:anObject]) {
  NSLog(@"Got it!");
} else {
  NSLog(@"Don't have it.");
}

Alternatively, you can use the containsObject: method to achieve the same thing:

if ([anArray containsObject:aExerciseRef.IDE]) {
  NSLog(@"Got it!");
} else {
  NSLog(@"Don't have it.");
}

The second option will not give you the index of the object, but that is easily rectified using:

NSInteger index = NSNotFound;
if ([anArray containsObject:aExerciseRef.IDE]) {
  index = [anArray indexOfObject:aExerciseRef.IDE];
  ...
}
Dave DeLong