Objective-C formatting string for boolean?

后端 未结 9 1725
情深已故
情深已故 2020-12-13 11:38

What formatter is used for boolean values?

EDIT:

Example: NSLog(@\" ??\", BOOL_VAL);, what is ?? ?

相关标签:
9条回答
  • Just add the below function and pass it the BOOL value and method will return back the NSString

    - (NSString *)boolValueToString:(BOOL)theBool {
        if (theBool == 0)
            return @"NO"; // can change to No, NOOOOO, etc
        else
            return @"YES"; // can change to YEAH, Yes, YESSSSS etc
    }
    
    0 讨论(0)
  • 2020-12-13 12:28

    I believe the easiest way to do this is:

    NSLog(@" %@", @(BOOL_VAL));
    

    @(expression)

    Dynamically evaluates the boxed expression and returns the appropriate object literal based on its value (i.e. NSString for const char*, NSNumber for int, etc.).

    0 讨论(0)
  • 2020-12-13 12:29

    Use the integer formatter %d, which will print either 0 or 1:

    NSLog(@"%d", myBool);
    
    0 讨论(0)
提交回复
热议问题