Pages

June 18, 2013

NSString to BOOL

A helpful function converting a NSString object into a BOOL (aka boolean).


/** Convert a string to its boolean representation.

 @param str String to transform.
 @return Returns the boolean value detected from parsing the string.
 @see stringFromBool:ofType:

 */
+ (BOOL)boolFromString:(NSString *)str {

    NSString *str2 = [str uppercaseString];
    if ([str2 isEqualToString:@"YES"]) {
        return YES;
    } else if ([str2 isEqualToString:@"TRUE"]) {
        return YES;
    } else if ([str2 isEqualToString:@"1"]) {
        return YES;
    } else {
        return NO;
    }
}



 

Download the ready-for-use source file (.m) of NSString to BOOL

No comments:

Post a Comment