如何从字典中访问字符串

时间:2014-06-10 04:36:37

标签: ios objective-c

我从Web服务获得以下响应,我需要获取字符串

  

错误:数据库中找不到记录。

如下所示:

d =     (
    "Error: no record found in database."
);

这是我的代码

 if([[[[dictiona objectForKey:@"d"] objectForKey:@""] objectAtIndex:0] isKindOfClass:[NSString class]] == YES)
    {
        UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"Message" message:@"No Record Found"  delegate:nil  cancelButtonTitle:@"OK" otherButtonTitles: nil];

        [myAlertView show];
}

1 个答案:

答案 0 :(得分:1)

您的代码不应该检查第二个objectForKey,因为没有要检查的第二个NSDictionary,因为您的第二个项目是NSArray,它不会通过密钥响应。这应该适合你:

if ([[[dictionary objectForKey:@"d"] objectAtIndex:0] isKindOfClass:[NSString class]])
{
    UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"Message" message:@"No Record Found"  delegate:nil  cancelButtonTitle:@"OK" otherButtonTitles: nil];

    [myAlertView show];
}