silverkid
silverkid

Reputation: 9573

How to perform a "select" on an array in objective c?

I have an array in objective c whose contents are this:

 reg_nr = 0002,          valor = 00010,       nomtech  =  ab-dd 
 reg_nr = 0002,          valor = 00020,       nomtech  =  ab-dt 
 reg_nr = 0003,          valor = 00030,       nomtech  =  ab-ds 
 reg_nr = 0003,          valor = 00040,       nomtech  =  ab-jh 

I want to get a value out of this array just like a select statement.

e.g if this were a select statement which column names

reg_nr   valor   nomtech
0002     00010   ab-dd
0002     00020   ab-dt
0003     00030   ab-ds
0003     00040   ab-jh

The select query would be select nomtech from tablename where valor = '00030' and I would get the value ab-ds.

How do I do this for the array above?

Edit - Clarification - this is show the array is formed -

SUPObjectList *docs2 = [MMRDS_QSSCabeceraPedido findAll];

// findall is a sap command that returns a list of objects 

    if ([docs2 length] > 0)
    {

        for (MMRDS_QSSCabeceraPedido * oneRec in docs2)
        {
            [array1 addObject:oneRec];

        }

    }

Upvotes: 0

Views: 1229

Answers (2)

Greg
Greg

Reputation: 25459

You can use NSPredictate like that:

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF contains %@", @"valor = 00030"];
    NSArray *filterArray = [array filteredArrayUsingPredicate:predicate];
    NSLog(@"ARRAY: %@", filterArray);

I gives you whole object, if you want to get just nom tech you can get the string object from the filtered array and you can spoilt it by ','

NSArray *splitArray = [filterArray[0] componentsSeparatedByString:@","];

Now in you can get your nom tech by call

NSString *string = splitArray[2];

This string will contain nomtech = ab-dd so you have to remove the substring you don't need. Hope this help.

Upvotes: 1

Vinzzz
Vinzzz

Reputation: 11724

Have a look at NSPredicate , it is very similar to 'select' statement to filter an array

Upvotes: 2

Related Questions