-1

Possible Duplicate:
Iterar over array of entity and get value of some key based on value of “other key” matche

Please, can anyone give me a hint or tip on how I can get the value of all the salary fields in my dictionary, if the status field is not null? I only want to retrieve the salary field from those objects in the dictionary where status != null. Note that my dictionary is dynamic, meaning I may have mor than four entries. Any help or hints highly appreciated. Thank you in advance.

myArray=(
        {
        Name = "john1";
        Address = "san diego";
        Status = "active";
        salary = "100;

    },
        {
        Name = "steve ";
        Address = "birmingham";
        Status = "<null>";
        salary = "100;
    },
        {
         Name = "allan";
        Address = "san diego";
        Status = "active";
        salary = "100;
    },

     {
         Name = "peter";
        Address = "san diego";
        Status = "<null>";
        salary = "100;
    },
)
Community
  • 1
  • 1
baste
  • 827
  • 2
  • 9
  • 18

4 Answers4

0

Try fast enumeration...

NSMutableArray* retreivedSalaries;

for (NSDictionary* dict in myArray) {
    if (![[dict objectForKey:@"Status"] isEqualToString:@"<null>"])
        [retreivedSalaries addObject:[dict objectForKey:@"salary"]];
 };
foundry
  • 31,615
  • 9
  • 90
  • 125
0

I have not tested so you may need to change this

for (int i=0;i< [myArray count];i++)
{
    if ([[[myArray objectAtIndex: i] objectForKey: @"Status"] isEqualToString: @"active"]) {

       NSLog(@"Salary is %@",[myArray objectAtIndex: i] objectForKey: @"salary"] )
    } else {
        NSLog(@"Status is not active");
    }
}

or you can use something like this

  for (NSDictionary *salary in myArray)
    {
        if([message valueForKey:@"Status"] isEqualToString: @"active")
           NSLog(@"Salary is %@",[message valueForKey:@"salary"]);      

    }

Sorting salary

You can sort the keys and then create an NSMutableArray by iterating over them. answer from here

NSArray *sortedKeys = [[myArray allKeys] sortedArrayUsingSelector: @selector(compare:)];
NSMutableArray *sortedValues = [NSMutableArray array];
for (NSString *key in sortedKeys)
    [sortedValues addObject: [myArray objectForKey: key]];
Community
  • 1
  • 1
SpaceDust__
  • 4,844
  • 4
  • 43
  • 82
  • the last one concept works! :D i just need to declare a NSString variable first and that variable will be the test inside the condition. thank you! (y) – baste Dec 21 '12 at 00:39
  • is there a to sort the value of the salary? – baste Dec 21 '12 at 01:08
  • sure you need to sort and add the value to a new nsmutable array, I have edited my answer look at it, and accept the answer if it had helped you – SpaceDust__ Dec 21 '12 at 14:56
  • Space Dust. thank you for your time and effort but what is allKeys? – baste Dec 22 '12 at 16:46
0

Try this:

  NSDictionary *salaries = nil;
  NSArray *myArray .... ;


for (NSString * salary in salaries) {

    if ([myArray containsObject:@"salary"]) {

        if ([salaries objectForKey:@"salary"] != NULL) {

            NSLog (@"do something with salary: %@", salary);
        }
    }

}
neowinston
  • 7,584
  • 10
  • 52
  • 83
0

I Think this is iOS, myArray is an NSArray, and each dinamically person info is a NSDictionary, if this is the point, you can do the following:

for (NSDictionary *info in myArray) {

    if ([info objectForKey:@"Status"] != nil && ![[info objectForKey:@"Status"] isEqual:@"<null>") {
        // Store salary whatever you need and in the formar you need
        NSString *salary = [info objectForKey:@"salary"]; 
    }

}

I wrote the code without a compiler, so maybe there is some compilators errors (sorry). If I understand well your question, with that you can get the salary when the Status is not null.

Hope Helps!

Francisco Hernandez
  • 2,378
  • 14
  • 18