PDA

View Full Version : seeing if an object exists at index[i] in NSMUtableArray




ulquiorra
Sep 15, 2009, 10:47 AM
People I'd like to see if an element at index 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:

Let me elaborate a bit more
I'm (again ) using XML parser for this one;).

@HarryPotter thanks for your answer but that's not what I want to achieve.

@dejo aExerciseRef is a class I'm using to fill different attributes ( just like the Books class in the this example http://www.iphonesdkarticles.com/2008/11/parsing-xml-files.html )

example


if( aworkout != nil )
{
aExerciseRef = [[exerciseref alloc] init];
aExerciseRef.IDE = [attributeDict objectForKey:@"id"];
aExerciseRef.header = [attributeDict objectForKey:@"header"];


To elaborate a little further

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]note: I'm using the same array here so not a different one that I'm filling. So i'm actually comparing if AN aExerciseRef.IDE is present at location index[i].



dejo
Sep 15, 2009, 11:16 AM
First, what are the datatypes of aExerciseRef and aExerciseRef.IDE?

HairyPotter
Sep 15, 2009, 11:37 AM
no, you use

if ([yourArray containsObject:myObject])
yes, the array contains the object
else
no... the object is not on the array...

ulquiorra
Sep 16, 2009, 08:33 AM
I've done some editing in my post.

dejo
Sep 16, 2009, 10:59 AM
I've done some editing in my post.
Next time just add a new post, if there already are reply posts.

dejo
Sep 16, 2009, 11:08 AM
@dejo aExerciseRef is a class I'm using to fill different attributes ( just like the Books class in the this example http://www.iphonesdkarticles.com/2008/11/parsing-xml-files.html )

example


if( aworkout != nil )
{
aExerciseRef = [[exerciseref alloc] init];
aExerciseRef.IDE = [attributeDict objectForKey:@"id"];
aExerciseRef.header = [attributeDict objectForKey:@"header"];


To elaborate a little further

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.

note: I'm using the same array here so not a different one that I'm filling. So i'm actually comparing if AN aExerciseRef.IDE is present at location index[i].
So you have two arrays and you want to see if the objectAtIndex:i from the first array exists (somewhere) in the second array? Is that correct?

ulquiorra
Sep 16, 2009, 12:21 PM
yes dejo that's correct.

And what I did was create something like this


for( int i = 1; i <= 5; i++ )
{
for( int j = 0; j < [aExerciseRefarray count]; j++ )
{
if([instruct objectAtIndex:index2 + i] != [aExerciseRefarray objectAtIndex:j]){
...


with the aExerciseRefArray filled with aExerciseRef.IDE's.

However this doesn't seem to work.