User558 User558 - 26 days ago 16
Objective-C Question

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

I have two dictionary with multiple items. I want to compare values.

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

Dict2:{
"Displacement_trim" = "171.20 ";
"Dry_Weight" = "<null>";
}



  1. I want to know which "Displacement_trim" is greater.

  2. Also checking null values.

  3. Print the data in cell.



How can I achieve this?

Answer

To find out which value from the dict is bigger use the following code:

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
}
Comments