2

I'm currently trying to get hold of my NSMutableArray's index depending on one of it's object's properties.

I have an xml-structure such as this:

<timeperiod>
    <Day>
        <Date>20120325</Date>
    </Day>
    <Day>
        <Date>20120326</Date>
    </Day>
</timeperiod>

Now, imagine that the xml-structure contains every day in a month.

Day and all of it's properties are added to an NSMutableArray.

And what i want is: To get the array's index depending on the date.

I'm imagining something like this in pseudo-code:

-(NSInteger)getIndexFromObjectProperty:(NSString *)property{

   // Givf index from array where date is for example 20120310

   //pseudo
   Return [myClassObject.ObjectArray indexFromProperty:property];


}

I found this: Sorting NSMutableArray By Object's Property

This sorts from properties, but how to get an index from a property 0_0

I also found this: Function to get index from an array of objects having certain value of provided property Which would help alot if the syntax worked out of the box :(

Any tips and/or pointers will be highly appreciated.

Thanks in advance.

EDIT I'm currently trying the answers given. Will get back with an update.

EDIT2: Office is throwing me out since they're locking the building down... i'll get back to you tomorrow..

Community
  • 1
  • 1
Jens Bergvall
  • 1,617
  • 2
  • 24
  • 54

5 Answers5

6

You can use indexOfObjectPassingTest: method to test each object with a block returning a BOOL.

-(NSInteger)getIndexFromObjectProperty:(NSString *)property{
    return [myArray indexOfObjectPassingTest:
        ^(id obj, NSUInteger idx, BOOL *stop) {
            // Return true if obj has the date that you are looking for.
            BOOL res;
            if ([property isEqualTo:[obj dateProperty]]) {
                res = YES;
                *stop = YES;
            } else {
                res = NO;
            }
            return res;
        }];
 }
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
6

This really was not as hard as i thought it would be..

solved by doing the following...

-(NSInteger)returnIndexFromDateProperty:(NSString *)property{


    NSInteger iIndex;

    for (int i = 0; i < [myArray count]; i++){

        if([property isEqualToString:[[myArray objectAtIndex:i]dateProperty]]){

            iIndex = i;
        }
    }    


    return iIndex;

}

Thanks for all the other answers. They hepled alot during the process of thinking (not being bad).

Jens Bergvall
  • 1,617
  • 2
  • 24
  • 54
2

You can use

 - (NSUInteger)indexOfObjectPassingTest:(BOOL (^)(id obj, NSUInteger idx, BOOL *stop))predicate

I'm not sure how your date format is, but as I understood it's an NSString, so I'd do it like this :

-(NSInteger)indexFromDate:(NSString *)date{

   return [myClassObject.ObjectArray indexOfObjectPassingTest:
             ^(id obj, NSUInteger idx, BOOL *stop) {
                return ([obj isEqualToString:myIdentifier]);
             }];
}
Julien
  • 9,312
  • 10
  • 63
  • 86
1

You can do this with NSPredicate

[array indexOfObject:[[array filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"SELF.date == %@",date]] objectAtIndex:0]]
Vignesh
  • 10,205
  • 2
  • 35
  • 73
0

It's not clear from your post what structure your array will have after putting the XML data into it. Will it be an array of arrays? An array of dictionaries? Generally, to get an object from an array you use indexOfObject: or one of its variations.

rdelmar
  • 103,982
  • 12
  • 207
  • 218