Header Ads

NSCFString Vs NSString String Comparison in Conditional Statements

Recently I have come across with a problem of string (NSString/NSCFString) comparison in a IF Condition.

I got a solution and check the solution below.

I have declared a Variable of type NSString and tried to retrieve some value to this which is returning NSCFString.

NSString *retVal;

retVal = //Some routine which will return NSCFString.

You will not get any build and runtime errors as the variable is "NSString" and return value is some thing different "NSCFString" but if you use this returned value in conditional statements it will not work.

For suppose retVal is "Enabled".

if(retVal == @"Enabled"){

//Enabled }

else {

//Disabled }

In the above case even retVal is "Enabled" it will not go to the Enabled Section

To resolve the above issue we need to follow the following code snippet,

if([retVal isEqualToString: @"Enabled"]){

//Enabled}

else {

//Disabled]; }

No comments:

Powered by Blogger.