I am trying to learn how to use API's with Objective-C. I am using the data from here: https://btc-e.com/api/2/ltc_usd/ticker and I only want the 'last' value. I have tried extracting the value like this:
NSURL * url=[NSURL URLWithString:@"https://btc-e.com/api/2/ltc_usd/ticker"];
NSData * data=[NSData dataWithContentsOfURL:url];
NSError * error;
NSMutableDictionary * json = [NSJSONSerialization JSONObjectWithData:data options: NSJSONReadingMutableContainers error: &error];
NSArray *keys = [json allKeys];
NSString *jsonStr = [json objectForKey:keys[0]];
NSArray *c1 = [jsonStr componentsSeparatedByString:@"last = \""];
NSArray *c2 = [[c1 objectAtIndex:1] componentsSeparatedByString:@"\";"];
NSString *result = [c2 objectAtIndex:0];
NSLog(@"%@", result);
However, this gives me the following errors:
2014-03-02 15:03:24.915 Litecoin Ticker[5727:303] -[__NSDictionaryM componentsSeparatedByString:]: unrecognized selector sent to instance 0x608000240690
2014-03-02 15:03:24.915 Litecoin Ticker[5727:303] -[__NSDictionaryM componentsSeparatedByString:]: unrecognized selector sent to instance 0x608000240690
I'm not entirely sure this is only way to extract values from API's, but i can't seem to find out how to do it. Can anyone help?
componentsSeparatedByString. Just reference the NSDictionarys usingobjectForKey(or the new form using[]).NSNumber* last = json[@"ticker"][@"last"];errorvalue.)