iOS: - [__ NSCFString allKeys]:无法识别的选择器发送到实例0x7976c750

时间:2015-04-05 16:56:15

标签: ios objective-c json crash nsdictionary

现在我知道这意味着它试图获取NSString中的所有密钥,这是不可能的。但是,我不知道为什么我的字典是NSString。

return [requestManager POST:@"/api/showData"
                           parameters:params
                              success:^(AFHTTPRequestOperation *operation, id responseObject) {

                                  NSLog(@"the response object%@",responseObject);

                                  NSMutableArray *responseObjects = [[NSMutableArray alloc] init];
                                  for (NSDictionary *node in responseObject) {
                                      Model *data = [[Model alloc] initWithDictionary:node];
                                      [responseObjects addObject:data];
                                  }
                                  handler(YES,responseObjects);

                              } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                                  handler(NO,nil);
                              }];

此代码向服务器发送请求,我得到以下成功响应

  

响应对象{           “error_code”= 1;           message =“找不到”;           status =错误;          }

在我的Model类的initWithDictionary方法中,这就是我所拥有的

   //Model Class
 - (id)initWithDictionary:(NSDictionary *)dict{
      self = [super init];
      if (self) {
       NSLog(@"the dict is %@",dict);
      if ([[dict allKeys] containsObject:@"error_code"]) {

          }
         return self;
       }

以上一行是造成我问题的原因。我在这个方法中记录了字典,这就是我得到的

  

“dict is status”

1 个答案:

答案 0 :(得分:3)

您需要检查是否正在获取字典或数组(包含多个词典)作为responseObject,

return [requestManager POST:@"/api/showData"
                parameters:params
                   success:^(AFHTTPRequestOperation *operation, id responseObject) {

                       NSLog(@"the response object%@",responseObject);
                       NSMutableArray *responseObjects = [[NSMutableArray alloc] init];

                       if ([responseObject isKindOfClass:[NSDictionary class]]) {
                           Model *data = [[Model alloc] initWithDictionary:responseObject];
                           [responseObjects addObject:data];
                       } else if ([responseObject isKindOfClass:[NSArray class]]) {
                           for (NSDictionary *node in responseObject) {
                               Model *data = [[Model alloc] initWithDictionary:node];
                               [responseObjects addObject:data];
                           }
                       }
                       handler(YES,responseObjects);

                   } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                       handler(NO,nil);
                   }];