将NSMuableDictionary中的数据提取到NSString并转换为String问题

时间:2012-04-27 04:02:06

标签: iphone ios variable-assignment nsmutabledictionary

我将NSMuableDictionary中的数据提取到NSString并尝试像这样编译String:

   NSDictionary *error = [[NSDictionary alloc]init];
   NSString *errorCode = [[NSString alloc]init];
   error = [sing.globalCallsDitionary valueForKey:@"Error"];
   NSLog(@"error code %@",[error valueForKey:@"error_code"]);
   errorCode = [error valueForKey:@"error_code"];
   if ([errorCode isEqualToString:@"-1"]) {

当if语句执行时,我在讨论数组时会出现此错误,错误如下所示:

2012-04-27 06:34:49.686 CallBiz[10319:707] error code (
    "-1"
)
2012-04-27 06:35:00.602 CallBiz[10319:707] -[__NSArrayI isEqualToString:]: unrecognized selector sent to instance 0x16fc10
2012-04-27 06:35:00.608 CallBiz[10319:707] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI isEqualToString:]: unrecognized selector sent to instance 0x16fc10'
*** First throw call stack:
(0x3541f88f 0x36846259 0x35422a9b 0x35421915 0x3537c650 0xb2a3 0x353793fd 0x32458faf 0x32458f6b 0x32458f49 0x32458cb9 0x324595f1 0x32457ad3 0x324574c1 0x3243d83d 0x3243d0e3 0x3667222b 0x353f3523 0x353f34c5 0x353f2313 0x353754a5 0x3537536d 0x36671439 0x3246be7d 0x2e61 0x28fc)
terminate called throwing an exception

正如xCode正在查看我的NSString *errorCode = [[NSString alloc]init];,好像它是NSArray一样,有人可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:2)

发生错误是因为您的errorCode是一个数组,而不是字符串。如果您确定只有一个对象来到这里,那么您可以使用 -

if ([[errorCode objectAtIndex:0] isEqualToString:@"-1"])

它会起作用,虽然它会通过警告,因为errorCode被定义为一个字符串对象。因此,您需要确定响应中的数据,然后适当地定义数据结构。