-1

I'm retrieving a value from JSON in NSArray format. It's temperature.

However it is in Celsius and I wish to convert it to Kelvin. All I need to do is add 274.15. I'm not really able to do that since it's NSArray and I can't add directly.

I tried converting to NSString and then converting to double however it seems like I don't have that option.

This is the current code:

NSDictionary *temp = [currentDate objectForKey:@"temp"];

        NSArray* maxTemp = [temp objectForKey:@"max"];
        NSLog(@"Max Temp  : %@ Kelvin",maxTemp );
        NSArray* minTemp = [temp objectForKey:@"min"];
        NSLog(@"Min Temp  : %@ Kelvin",minTemp);

All I wanna do is get ( maxTemp + 274.15 )

Current values in Celsius:

Max Temp  : 28.8 Kelvin
Min Temp  : 26.55 Kelvin
  • 1
    "since it's NSArray" - o rly? `objectForKey:` returns an `id` (which is alias for `NSObject*`). Just because you decided to treat it as NSArray, does not mean that it **is** an NSArray. That log does not look like NSArray, to start with. – Kreiri Jun 25 '14 at 23:41
  • I'm sorry, didn't realize I could use NSString for it. I'm retrieving a very raw kind of JSON data which hasn't been formatted properly so somewhere during retreviing individual values I ended up using NSArray. – user3768711 Jun 26 '14 at 19:08

1 Answers1

2

Since your log statements are displaying numeric values, you can tell that the contents of the dictionary "max" and "min" keys are NOT arrays. They are almost certainly strings. (JSON sends data as strings.)

NSString has a method doubleValue. Try this:

NSDictionary *temp = [currentDate objectForKey:@"temp"];

NSString* maxTempString = [temp objectForKey:@"max"];
double maxTemp = [maxTempString doubleValue];
NSLog(@"max Temp  : %f Kelvin", maxTemp );

NSString* minTempString = [temp objectForKey:@"min"];
double minTemp = [minTempString doubleValue];
NSLog(@"Min Temp  : %f Kelvin", minTemp);
Duncan C
  • 128,072
  • 22
  • 173
  • 272