来自nsdictionary的Gettnig最大值

时间:2016-01-04 04:22:18

标签: objective-c nsdictionary

这是我的代码,  NSMutableDictionary * dictstud1marks = [NSMutableDictionary dictionary];

[dictstud1marks setObject:@40 forKey:@"Maths"];
[dictstud1marks setObject:@50 forKey:@"English"];
[dictstud1marks setObject:@60 forKey:@"Science"];

NSMutableDictionary *dictstud2marks = [NSMutableDictionary dictionary];

[dictstud2marks setObject:@50 forKey:@"Maths"];
[dictstud2marks setObject:@60 forKey:@"English"];
[dictstud2marks setObject:@80 forKey:@"Science"];


NSMutableDictionary *dictstud3marks = [NSMutableDictionary dictionary];

[dictstud3marks setObject:@70 forKey:@"Maths"];
[dictstud3marks setObject:@80 forKey:@"English"];
[dictstud3marks setObject:@90 forKey:@"Science"];


NSMutableDictionary *dictstud =[NSMutableDictionary dictionary];

[dictstud setObject:dictstud1marks forKey:@"Ram"];
[dictstud setObject:dictstud2marks forKey:@"Rohit"];
[dictstud setObject:dictstud3marks forKey:@"Saraf"];

我想知道如何从这本字典中获取最大值。请帮助我解决。 提前谢谢,

3 个答案:

答案 0 :(得分:0)

在循环中尝试沙地回答将提供您的预期结果。

gunicorn  app.wsgi:application -b 127.0.0.1:8000 --daemon

答案 1 :(得分:0)

NSMutableDictionary  *maximumDictFinal= [[NSMutableDictionary alloc] init];
NSString  *marks;
NSArray *temp = [dictstud allKeys];
for (int i=0 ; i<[temp count]; i++) {
    NSMutableDictionary *maximumDict = [[NSMutableDictionary alloc] init];

    NSArray *blockSortedKeys = [[dictstud objectForKey:[temp objectAtIndex:i]] keysSortedByValueUsingComparator: ^(id obj1, id obj2) {
        // Switching the order of the operands reverses the sort direction
        return [obj2 compare:obj1];
    }];
     marks =  [[[dictstud objectForKey:[temp objectAtIndex:i]]allValues] valueForKeyPath:@"@max.intValue"];
    [maximumDict setObject:marks  forKey:[blockSortedKeys objectAtIndex:0]];
    [maximumDictFinal setObject:maximumDict forKey:[temp objectAtIndex:i]];

       }
NSLog(@"maximumDict..%@",maximumDictFinal);

根据您的评论

    int maximum =0;
int prevMaximum ;
for (NSDictionary *dict in dictstud)
{

   prevMaximum =[[[[dictstud objectForKey:dict]allValues] valueForKeyPath:@"@max.intValue"] intValue];
    if (prevMaximum >maximum) {
        maximum =prevMaximum;

    }

}
 NSLog(@"maximum marks of student ..%d",maximum);

答案 2 :(得分:-1)

尝试一次:

NSLog(@"maximum marks of student ..%@",[[[dictstud objectForKey:@"Saraf"]allValues] valueForKeyPath:@"@max.intValue"]);