convert NSTaggedPointerString to NSString

别等时光非礼了梦想. 提交于 2019-11-29 09:08:35
Catfish_Man

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

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

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

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;

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);

 }

for Swift 3 or 4

String(stringInterpolationSegment: taggedPointerString) 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!