可能重复:
Iterar over array of entity and get value of some key based on value of “other key” matche
请问,如果状态字段不为空,那么任何人都可以给我一个关于如何获取字典中所有salary
字段值的提示或提示吗?我只想从salary
字典中的那些对象中检索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;
},
)
答案 0 :(得分:0)
尝试快速枚举...
NSMutableArray* retreivedSalaries;
for (NSDictionary* dict in myArray) {
if (![[dict objectForKey:@"Status"] isEqualToString:@"<null>"])
[retreivedSalaries addObject:[dict objectForKey:@"salary"]];
};
答案 1 :(得分: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。 answer from here
NSArray *sortedKeys = [[myArray allKeys] sortedArrayUsingSelector: @selector(compare:)];
NSMutableArray *sortedValues = [NSMutableArray array];
for (NSString *key in sortedKeys)
[sortedValues addObject: [myArray objectForKey: key]];
答案 2 :(得分: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);
}
}
}
答案 3 :(得分:0)
我认为这是iOS,myArray是一个NSArray,每个dinamically人信息都是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"];
}
}
我没有编译器编写代码,所以也许有一些编译器错误(抱歉)。如果我很清楚你的问题,那么当状态不为空时,你可以获得工资。
希望帮助!