-1

可能重复:
迭代实体数组并根据“其他键”匹配的值获取某个键的值

salary拜托,如果状态字段不为空,任何人都可以给我一个提示或提示,告诉我如何获取字典中所有字段的值吗?我只想salary从字典 where 中的那些对象中检索字段status != null。请注意,我的字典是动态的,这意味着我可能有四个以上的条目。非常感谢任何帮助或提示。先感谢您。

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;
    },
)
4

4 回答 4

0

尝试快速枚举...

NSMutableArray* retreivedSalaries;

for (NSDictionary* dict in myArray) {
    if (![[dict objectForKey:@"Status"] isEqualToString:@"<null>"])
        [retreivedSalaries addObject:[dict objectForKey:@"salary"]];
 };
于 2012-12-20T18:25:52.353 回答
0

我没有测试,所以你可能需要改变这个

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");
    }
}

或者你可以使用这样的东西

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

    }

排序工资

您可以对键进行排序,然后通过迭代它们来创建 NSMutableArray。 从这里回答

NSArray *sortedKeys = [[myArray allKeys] sortedArrayUsingSelector: @selector(compare:)];
NSMutableArray *sortedValues = [NSMutableArray array];
for (NSString *key in sortedKeys)
    [sortedValues addObject: [myArray objectForKey: key]];
于 2012-12-20T18:27:17.373 回答
0

尝试这个:

  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);
        }
    }

}
于 2012-12-20T18:27:37.247 回答
0

我认为这是 iOS,myArray 是一个 NSArray,每个人的信息都是一个 NSDictionary,如果这是重点,您可以执行以下操作:

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"]; 
    }

}

我在没有编译器的情况下编写了代码,所以可能存在一些编译器错误(抱歉)。如果我很好地理解了您的问题,那么您可以在 Status 不为空时获得薪水。

希望有帮助!

于 2012-12-20T18:49:56.050 回答