convert NSTaggedPointerString to NSString
Asked Answered
A

5

22

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 use NSString in module to store it and then print the class name of this sex property, it printed as NSTaggedPointerString, I doubt how to convert it to a String to use it. Anyone have good ideas?

Antisepticize answered 19/10, 2015 at 5:44 Comment(1)
{ errNum = 0; retData = { address = "\U6e56\U5317\U7701\U7701\U76f4\U8f96\U53bf\U7ea7\U884c\U653f\U533a\U5212\U4ed9\U6843\U5e02"; birthday = "1989-07-10"; sex = M; }; retMsg = success; } This is the JSON that returnsAntisepticize
R
37

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

Rheta answered 19/10, 2015 at 5:53 Comment(8)
Thanks, I just find it as I used the wrong arguments in swift, it should use %@ just as Obj-C but I used the %s instead. I fixed the error.Antisepticize
In my cace,when does not convert than makes warnning \n "Incompatible pointer types initializing ~~"Sher
That's a different issue entirely. NSTaggedPointerString is about the dynamic type of the object. Your issue is about the type you have written in your source code.Rheta
po [NSClassFromString(@"NSTaggedPointerString") superclass] will prove your answer.Taken
@Rheta could you explain a little bit more about the the issue you described? Since I need to save the string into file but with NSTaggedPointerString it seemed not be saved correctly.Mendes
That sounds like something else. The issue I mentioned in the comments wouldn't apply to anything while the app is running, only when it's being compiled.Rheta
Try converting a number to a string and pass it on from one view controller to another, without doing something like stringWithString beforehand. You will see how "anywhere" is not actually anywhere.Repentance
I've done many things like that. It works fine. You have some other more subtle bug there that is being worked around.Rheta
M
11

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 #>];
Misunderstood answered 19/4, 2016 at 16:8 Comment(1)
I found it easier to just say NSDictionary *aDict = @{[myvar valueForKey:@"item"] since the .property doesn't work well. KVO to the rescueIzanagi
I
3

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;
Impetuosity answered 15/3, 2017 at 20:32 Comment(0)
R
0

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

 }
Retentive answered 27/3, 2016 at 5:59 Comment(0)
G
0

for Swift 3 or 4

String(stringInterpolationSegment: taggedPointerString) 
Gehlbach answered 22/6, 2018 at 18:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.