views:

49

answers:

1

I would like to know how, in Objective-C, how to tell if a string represents a boolean value. The [string boolValue] method will not work, because when I try to parse a string like [@"ERROR" boolValue] it returns NO instead of throwing an exception. In C#, I could do something like: if (Boolean.TryParse(string, out bool)), but this is not available in Objective-C as far as I know because the BOOL type is not object oriented. Will I have to write my own BOOL parser class? Or is there something I am missing (NSScanner for instance)?

+1  A: 

According to the documentation, boolValue "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."

If you're looking for something different, you'll need to write a little utility or use a category to get the job done.

If it's something quick, you could even resort to using NSString's -isEqualToString: method.

Reed Olsen
So I need to make my own custom parser?
Richard J. Ross III
I would in this situation.
Reed Olsen
That's what I have done, and I do believe that it will work for my situation, thank you for the response.
Richard J. Ross III