0

In my Parse cloud code, I have custom errors like this

response.error({ 
    "Error" : "DEAL EXPIRED",
    "Title" : "Sorry!",
    "Message" : "This deal is expired :(",
    "Action" : "Ok"
});

Because I want to show a UIAlertView, but I have a problem to read the dictionary. My code in app is:

NSDictionary *errorDictionary = [error userInfo];

if ([[errorDictionary objectForKey:@"Error"] isEqualToString:@"DEAL EXPIRED"]) {

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:
        [NSString stringWithFormat:@"%@",[errorDictionary objectForKey:@"Title"]]
         message:[NSString stringWithFormat:@"%@",[errorDictionary objectForKey:@"Message"]]
         delegate:nil
         cancelButtonTitle:[NSString stringWithFormat:@"%@",[errorDictionary objectForKey:@"Action"]]
         otherButtonTitles:nil];
    [alert show];

}

The log of errorDictionary is:

{"Error":"DEAL EXPIRED","Title":"Sorry!","Message":"This deal is expired :(","Action":"Ok"}

Thank you

Vins
  • 1,814
  • 4
  • 24
  • 40

1 Answers1

0

Because the printout of the string is null, it means that your if statement will always be false. Therefore [error userInfo] is not an NSDictionary object. You need to convert the JSON string to a dictionary before you can use it as a dictionary.

Here is an example if it is an NSData object.

NSDictionary * errorDictionary = [NSJSONSerialization JSONObjectWithData:[error userInfo] options:kNilOptions error:&error];

Here is the Apple documentation if it is a NSInputStream object.

There is more information on JSON to NSDictionary conversion here.

Community
  • 1
  • 1
dstudeba
  • 8,878
  • 3
  • 32
  • 41