Objective C try parse boolean
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开发者_StackOverflow社区, 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)?
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.
精彩评论