Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to parse NSString into BOOL in Objective-C?

I am programming in Objective-C for iOS. I would like to parse an object of type NSString into a scalar of type BOOL.

I have a value, and I know that it will either be @"YES" or @"NO", but that YES (or) NO value is NSString and I just want to change NSString into BOOL.

How can I do that?

Please answer me if you know.

Thanks for reading.

like image 479
Fire Fist Avatar asked Jan 17 '12 16:01

Fire Fist


2 Answers

I think it's this:

BOOL boolValue = [myString boolValue]; 
like image 50
bschultz Avatar answered Sep 21 '22 11:09

bschultz


You should probably use NSString's -boolValue. To quote the documentation directly:

[Returns t]he Boolean value of the receiver’s text. Returns YES on encountering one of "Y", "y", "T", "t", or a digit 1-9—the method ignores any trailing characters. Returns NO if the receiver doesn’t begin with a valid decimal text representation of a number.

That would seem to match your input cases.

like image 35
Tommy Avatar answered Sep 24 '22 11:09

Tommy