convert NSTaggedPointerString to NSString

后端 未结 5 2160
夕颜
夕颜 2020-12-18 18:36

I had called an interface of Baidu to check id Number, however the value of Sex returned with sex = M, without \"\" around the M in JSON, when I us

相关标签:
5条回答
  • 2020-12-18 19:12

    for Swift 3 or 4

    String(stringInterpolationSegment: taggedPointerString) 
    
    0 讨论(0)
  • 2020-12-18 19:18

    In my case son {"count":"123"} I got error. Solved:

    // 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);
    
     }
    
    0 讨论(0)
  • 2020-12-18 19:21

    I have encountered places where NSTaggedPointerString cannot be used as a NSString, such as when creating an NSDictionary. In those cases use stringWithString::

    NSString* <# myNSString #> = [NSString stringWithString:<# myNSTaggegedString #>];
    
    0 讨论(0)
  • 2020-12-18 19:21

    I had this same issue multiple times. It would seem that type inference for NSDictionary is not an exact science. What I do is specifically ask if the object responds to a particular method. For example if I am looping through parsing some JSON and I am attempting to access a value of type 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;
    
    0 讨论(0)
  • 2020-12-18 19:29

    NSTaggedPointerString is already an NSString, it's just a subclass. You can use it anywhere you can use an NSString, without conversion.

    0 讨论(0)
提交回复
热议问题