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]; }

2 comments:

  1. I do agree with all the ideas you’ve introduced on your post. They are really convincing and will certainly work. Still, the posts are very quick for newbies. May just you please extend them a bit from subsequent time? Thanks for the post. smm panel youtube

    ReplyDelete
  2. I think this is one of the most important information for me. And i am glad reading your article. But wanna remark on some general things, The website style is wonderful, the articles is really excellent : D. Good job, cheers Get well soon

    ReplyDelete

Powered by Blogger.