【问题标题】:NSTaggedPointerString objectForKey in objective-cObjective-c 中的 NSTaggedPointerString objectForKey
【发布时间】:2017-03-06 03:23:03
【问题描述】:

当我尝试从 JSON 结果中获取结果时。它会引发以下异常。

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSTaggedPointerString objectForKey:]: unrecognized selector sent to instance 0xa006449656c6f526'

我的代码。

 NSString *responseStringWithEncoded = [[NSString alloc] initWithData: mutableData encoding:NSUTF8StringEncoding];
id jsonObjects = [NSJSONSerialization JSONObjectWithData:
                  mutableData options:NSJSONReadingMutableContainers error:nil];
 for (NSDictionary *dataDict in jsonObjects) {   
  NSString *firstname = [dataDict objectForKey:@"FirstName"];
  }

上面的代码抛出一个NSException

我的 JSON 响应如下所示。

{
"IsExternal": 0,
"LoginId": 4,
"EmployeeId": 223,
"FirstName": "GharValueCA",
"RoleId": 4,
"LastName": null,
"Mobile": null,
"AgencyId": 100,
"BranchId": 74
}

任何帮助将不胜感激。

【问题讨论】:

  • -[NSTaggedPointerString objectForKey:]: unrecognized selector sent to 您正在对 NSString 对象使用 NSDictionary 方法 (objectForKey:)。这就是说的错误。 jsonObjectsNSDictionary,不需要对其进行 for 循环,而您的代码建议它是 NSArrayNSDictionary

标签: ios objective-c json nsjsonserialization


【解决方案1】:

根据 JSON 的定义,每个 JSON 包含一个对象(可以是包含其他对象的集合类型)。在您的情况下,您的文本以“{”开头,所以这是一本字典。 单个字典。

所以NSJSONSerialization 在读取该文件时会返回一个NSDictionary,其中包含 IsExternal、FirstName 等键下的值。

但是,您的代码在该字典上使用 for( ... in ... )(根据 NSDictionary 文档,它将遍历字典中的键,它们是字符串),但是您将这些字符串视为字典 再次。

因此,您应该直接使用jsonObjects 中的字典,而不是循环遍历字典,方法是在其上调用-objectForKey: 之类的东西。

【讨论】:

  • 会好奇为什么反对者投票否决了这个?有什么不对吗?
【解决方案2】:

有误会:

jsonObjects 已经是字典,立即将反序列化的对象分配给dataDict

NSDictionary *dataDict = [NSJSONSerialization JSONObjectWithData:mutableData 
                                                         options:0
                                                           error:nil]; 
                  // mutableContainers in not needed to read the JSON

枚举的对象是字符串、数字或<null>。您在导致错误的字符串上调用了objectForKey:

直接取名字(无循环)

NSString *firstname = dataDict[@"FirstName"];

或者你可以枚举字典

 for (NSString *key in dataDict) {
    NSLog(@"key:%@ - value:%@", key, dict[key]);
 }

【讨论】:

    【解决方案3】:

    你应该打电话

    [jsonObjects objectForKey:@"FirstName"];
    

    获取 FirstName 值。

    以下代码行(可能)返回NSDictionary,因此这是存储所有 json 值的容器。

    id jsonObjects = [NSJSONSerialization JSONObjectWithData:
                  mutableData options:NSJSONReadingMutableContainers error:nil];
    

    【讨论】:

    • objectForKey 将是一个字符串,在目标 C 中字符串不像 " ",它应该是 @" "
    • 你真的应该通过jsonObjects[@"FirstName"];使用速记
    【解决方案4】:

    试试这个代码:

    if ([jsonObjects isKindOfClass:[NSDictionary class]]) {
        NSString *firstname = [jsonObjects objectForKey:@"FirstName"];
    }
    

    因为你的 'jsonObjects' 是通用类型 'id' 所以只要检查它是否是 NSDictionary 类型然后在 if-block 中你可以通过 objectForKey 直接访问它:

    【讨论】:

      【解决方案5】:

      试试这段代码,希望对你有帮助,

         id jsonObjects = [NSJSONSerialization JSONObjectWithData:
                            mutableData options:NSJSONReadingMutableContainers error:nil];
          if([jsonObjects respondsToSelector:@selector(objectForKey:)]){
               NSString *firstname = [jsonObjects objectForKey:@"FirstName"];
          }
      

      【讨论】:

      • 这只会让它在给定的输入下什么都不做。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-17
      相关资源
      最近更新 更多