在我的Parse云代码中,我有像这样的自定义错误
response.error({
"Error" : "DEAL EXPIRED",
"Title" : "Sorry!",
"Message" : "This deal is expired :(",
"Action" : "Ok"
});
因为我想展示一个UIAlertView,但是我有一个问题就是阅读字典。我的应用程序代码是:
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];
}
errorDictionary的日志是:
{"Error":"DEAL EXPIRED","Title":"Sorry!","Message":"This deal is expired :(","Action":"Ok"}
谢谢
答案 0 :(得分:0)
因为字符串的打印输出为空,这意味着您的if
语句将始终为false。因此[error userInfo]
不是NSDictionary
对象。您需要将JSON字符串转换为dictionary
,然后才能将其用作dictionary
。
以下是一个NSData
对象的示例。
NSDictionary * errorDictionary = [NSJSONSerialization JSONObjectWithData:[error userInfo] options:kNilOptions error:&error];
Here is the Apple documentation如果是NSInputStream
对象。
有关JSON到NSDictionary转换的更多信息here。