Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert NSCFString to NSString

I am getting a dictionary from server

myDictionary = 
{
"rank":"1",
"color":"red",
"position":"middle"
}

Now I want to check the value of key "position" in if condition

I am doing it like this

if ([[myDictionary valueForKey:@"position"] isEqualToString:@"middle"]) {
        //do Some stuff
} else{
        //do some other stuff
}

but data type of [myDictionary valueForKey:@"position"] is _NSCFString, so it does not compare value properly and never goes in if loop even the value is correct.

how do I convert it into NSString so that I could compare it in if condition ?

I have seen these questions..

NSString instance reports its class as NSCFString

Getting an NSString from an NSCFString

NSString or NSCFString in xcode?

from these question I just came to know that NSString is really a container class for different types of string objects. Generally an NSString constructor does return an object that is actually of type NSCFString, which is a thin wrapper around the Core Foundation CFString struct.

but they didn't help me..and no one actually telling how to convert in into NSString, so please don't mark it as duplicate.

like image 465
Prashant Nikam Avatar asked Apr 18 '14 09:04

Prashant Nikam


People also ask

What is __ Nscfstring?

__NSCFString is one created during runtime via Foundation or Core Foundation, while __NSCFConstantString is either a CFSTR("...") constant or an @"..." constant, created at compile-time. Their interfaces are private. Treat them both as NSString and you should have no trouble. Follow this answer to receive notifications.

Is NSString UTF 8?

An NSString object can be initialized from or written to a C buffer, an NSData object, or the contents of an NSURL . It can also be encoded and decoded to and from ASCII, UTF–8, UTF–16, UTF–32, or any other string encoding represented by NSStringEncoding .

What is NSString in objective c?

(NSString *) is simply the type of the argument - a string object, which is the NSString class in Cocoa. In Objective-C you're always dealing with object references (pointers), so the "*" indicates that the argument is a reference to an NSString object.

What is NSString in swift?

NSString : Creates objects that resides in heap and always passed by reference. String: Its a value type whenever we pass it , its passed by value. like Struct and Enum, String itself a Struct in Swift.


1 Answers

You don't need to convert _NSCFString to NSString. As a subclass of NSString it's guaranteed to respond to -isEqualToString: (and every other method in NSString). Your issue is not coming from the string, likely it is coming from myDictionary. Try logging all the keys in the dictionary and make sure it is behaving as expected.

like image 175
Ephemera Avatar answered Oct 21 '22 13:10

Ephemera