winnicki winnicki - 2 months ago 13
iOS Question

Getting an NSString from an NSCFString

I read around S.O and did Google this a fair bit but I wasn't able to find an answer to my problem...

I have an

NSDictionary
which is returning
NSCFString's
and what I need is an NSString so that I can use its doubleValue function. I can't for the life of me figure out how to turn my
NSCFString
's into
NSString
s. I've tried the following with no success:

NSString* lng_coord = (NSString*)[dict objectForKey:key];
if ([lng_coord class] == [NSString class])
NSLog(@"lng coord is an exact match with NSString class");
else {
NSLog(@"lng coord doesn't match NSString class");
NSLog(@"The class of lng_coord is %@", [[dict objectForKey:key] class]);
}
NSLog(@"%@", lng_coord);
[CelebsAnnotation setLongitude:[lng_coord doubleValue]];


And this is the output from console:

lng coord doesn't match NSString class
The class of lng_coord is NSCFString
49.2796758

Answer

NSCFString is a private subclass of NSString, you can just use it as one.

Because of such patterns like class clusters, you shouldn't directly compare the classes here - use -isKindOfClass: instead:

if ([lng_coord isKindOfClass:[NSString class]]) {
    // ...
Comments