如何比较两个字典键值并在 iOS 的单元格中打印文本?

How to compare two dictionary key values and print text in cell in iOS?

我有两个包含多个项目的字典。我想比较值。

Dict1:{
"Displacement_trim" = "49.26 ";
"Dry_Weight" = "<null>";
}

Dict2:{
"Displacement_trim" = "171.20 ";
"Dry_Weight" = "<null>";
} 
  1. 我想知道哪个 "Displacement_trim" 更大。
  2. 同时检查空值。
  3. 打印单元格中的数据。

我怎样才能做到这一点?

要找出字典中哪个值更大,请使用以下代码:

Swift 3

let dict1 = [ "Displacement_trim" : "49.26", "Dry_Weight" : "<null>" ]
let dict2 = [ "Displacement_trim" : "171.20", "Dry_Weight" : "<null>" ]

// I force unwrapped everything for brevity
if Float(dict1["Displacement_trim"]!)! > Float(dict2["Displacement_trim"]!)! {
    // highlight label for dict1
} else {
    // highlight label for dict2
}

Objective-C

NSDictionary *dict1 = @{@"Displacement_trim" : @"49.26", @"Dry_Weight" : @"<null>"};
NSDictionary *dict2 = @{@"Displacement_trim" : @"171.20", @"Dry_Weight" : @"<null>"};

NSString *dict1DisplacementTrim = dict1[@"Displacement_trim"];
NSString *dict2DisplacementTrim = dict2[@"Displacement_trim"];

if (dict1DisplacementTrim.floatValue > dict2DisplacementTrim.floatValue) {
    // highlight label for dict1
} else {
    // highlight label for dict2
}
NSMutableDictionary *dict1,*dict2;

 dict1 = [[NSMutableDictionary alloc]init];
dict2 = [[NSMutableDictionary alloc]init];

[dict1 setObject:[NSNumber numberWithFloat:13.20] forKey:@"Displacement_trim"];

[dict2 setObject:[NSNumber numberWithFloat:10.20] forKey:@"Displacement_trim"];

if ( [[dict1 valueForKey:@"Displacement_trim"] compare:[dict2 valueForKey:@"Displacement_trim"]]==NSOrderedAscending) {
    NSLog(@"dict 2 is greater");
}else{
    NSLog(@"dict 1 is greater");
}

如果有任何问题,请告诉我。