55

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.

Fire Fist
  • 7,032
  • 12
  • 63
  • 109

5 Answers5

157

I think it's this:

BOOL boolValue = [myString boolValue];
bschultz
  • 4,244
  • 1
  • 25
  • 33
  • what will be the boolValue of "Abdul"? Please suggest me. – Abdul Yasin Mar 04 '14 at 10:19
  • 3
    @Abdul Yasin, it will be 0/false. This method is only effective if you know there is a boolean value in the string; e.g. @"1", @"true", @"YES" etc. Other than these, boolValue will always return 0; – x4h1d Jul 16 '14 at 09:29
  • @x4h1d Thanks. I really appreciate your feedback – Abdul Yasin Jul 16 '14 at 09:32
  • And why `string 0` gives me `nil`, @x4h1d? – new2ios Jul 13 '15 at 14:32
  • @new2ios, `[@"string 0" boolValue]` returns `0`, not `nil`, as I said. You can try it using: `NSLog(@"%zd", [@"string 0" boolValue]);` in __Objective C__ or `let str: NSString = "string 0" println(str.boolValue)` in __Swift__. – x4h1d Jul 13 '15 at 15:34
  • Sorry, I don't explained well - I mean `[@"0" boolValue]` returns `nil`, @x4h1d. I have expression which returns string 0 (`@"0"`). When I convert it to `BOOL` using `boolValue` I get `nil`. – new2ios Jul 13 '15 at 15:36
  • @new2ios, are you sure? Because I just tested `[@"0" boolValue]` on both language and it returns as expected. FYI, make sure you use `%zd` to print boolValue. – x4h1d Jul 13 '15 at 15:44
  • 1
    :) it's okay. `po` means `print object` and `boolValue` is clearly not an object. – x4h1d Jul 13 '15 at 15:50
22

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.

Tommy
  • 99,986
  • 12
  • 185
  • 204
2
if ([string isEqualToString: @"YES"])
  foo();
else
  bar();
  • LOL u beat me by 6 seconds! haha – Steve Jan 17 '12 at 16:32
  • 1
    That's not the right way. See the other two answers by bschultz and Tommy. –  Jan 17 '12 at 16:34
  • 3
    @H2CO3, eh. The OP said `I exactly know the return value` so I wouldn't go so far as to say it was wrong. Although I agree that `boolValue` seems more robust - and possibly faster by virtue of only needing to inspect the first character... but still. – Steve Jan 17 '12 at 16:37
0

This property should also return true if string is 'true' that's why i think extension is needed...

extension NSString{
 var boolValueExtended: Bool {
    get{
        return boolValue ||
            self.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet()).uppercaseString == "TRUE"
    }
}

}

Yan
  • 1,569
  • 18
  • 22
0

This would probably best be solved with a conditional, for example:

if ([myString isEqualToString:@"YES"])
    myBool = YES;
else if ([myString isEqualToString:@"NO"])
    myBool = NO;

Hope this helped you, good luck with your programming.

Marcus Buffett
  • 1,289
  • 1
  • 14
  • 32
  • 1
    This solution is far from correct. There are numerous ways to store a bool in a string, and this only handles a very specific one. Why duplicate the functionality of a method that's already implemented. – FreeAsInBeer Mar 16 '12 at 13:14
  • This is too dirty way. – AlKozin Jan 16 '14 at 10:22