Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

seeing if an object exists at index[i] in NSMUtableArray

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.


1 Answers

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];
  ...
}
like image 135
Dave DeLong Avatar answered Jan 03 '26 14:01

Dave DeLong



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!