【问题标题】:convert NSTaggedPointerString to NSString将 NSTaggedPointerString 转换为 NSString
【发布时间】:2016-01-17 09:13:45
【问题描述】:

我调用了百度的接口查看id号,但是当我在模块中使用NSString存储它然后打印此sex 属性的类名,它打印为NSTaggedPointerString,我怀疑如何将其转换为字符串以使用它。谁有好的想法?

【问题讨论】:

  • { errNum = 0; retData = { 地址 = "\U6e56\U5317\U7701\U7701\U76f4\U8f96\U53bf\U7ea7\U884c\U653f\U533a\U5212\U4ed9\U6843\U5e02";生日 = "1989-07-10";性别 = M; }; retMsg = 成功;这是返回的 JSON

标签: objective-c json afnetworking-2


【解决方案1】:

我遇到过NSTaggedPointerString不能用作NSString的地方,比如创建NSDictionary的时候。在这些情况下使用stringWithString::

NSString* <# myNSString #> = [NSString stringWithString:<# myNSTaggegedString #>];

【讨论】:

  • 我发现说 NSDictionary *aDict = @{[myvar valueForKey:@"item"] 更容易,因为 .property 不能正常工作。 KVO 救援
【解决方案2】:

NSTaggedPointerString 已经是NSString,它只是一个子类。您可以在任何可以使用NSString 的地方使用它,无需转换。

【讨论】:

  • 谢谢,我只是发现它,因为我在 swift 中使用了错误的参数,它应该使用 %@ 就像 Obj-C 但我使用了 %s 代替。我修复了错误。
  • 在我的情况下,当不转换时会发出警告\n“不兼容的指针类型正在初始化~~”
  • 这完全是一个不同的问题。 NSTaggedPointerString 是关于对象的动态类型。您的问题与您在源代码中编写的类型有关。
  • po [NSClassFromString(@"NSTaggedPointerString") superclass] 会证明你的答案。
  • 这听起来像别的东西。我在 cmets 中提到的问题在应用程序运行时不适用于任何东西,只有在编译时才适用。
【解决方案3】:

适用于 Swift 3 或 4

String(stringInterpolationSegment: taggedPointerString) 

【讨论】:

    【解决方案4】:

    我多次遇到同样的问题。 NSDictionary 的类型推断似乎不是一门精确的科学。我所做的是专门询问对象是否响应特定方法。例如,如果我正在循环解析一些 JSON,并尝试访问 NSString 类型的值:

    NSString * string;
    if ([[dict objectForKey:@"value"] respondsToSelector:@selector(stringValue)]) {
        string = [[dict objectForKey:@"value"] stringValue];
    }
    else {
       string = [NSString stringWithString:[dict objectForKey:@"value"]];
    }
    documentFile.documentRevision = string;
    

    【讨论】:

      【解决方案5】:

      在我的情况下,儿子 {"count":"123"} 我出错了。 已解决:

      // Data was created, we leave it to you to display all of those tall tales!
      // NSLog(@«data: %@", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
      
      NSDictionary * json  = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
      if ([json isKindOfClass:[NSDictionary class]]){ //Added instrospection as suggested in comment.
                        NSArray *dicArray = json[@"count"];
                         NSLog(@"=:%@", dicArray);
      
       }
      

      【讨论】:

        猜你喜欢
        • 2019-07-23
        • 1970-01-01
        • 2015-10-30
        • 2016-01-22
        • 1970-01-01
        • 2017-03-08
        • 2012-02-29
        • 2011-02-04
        • 2011-04-24
        相关资源
        最近更新 更多