Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 191 Vote(s) - 3.47 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Objective-C formatting string for boolean?

#1
What formatter is used for boolean values?

EDIT:


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

#2
One way to do it is to convert to strings (since there are only two possibilities, it isn't hard):

NSLog(@" %s", BOOL_VAL ? "true" : "false");

I don't think there is a format specifier for boolean values.
Reply

#3
I would recommend

NSLog(@"%@", boolValue ? @"YES" : @"NO");

because, um, `BOOL`s are called `YES` or `NO` in Objective-C.
Reply

#4
Format strings for use with NSLog and [NSString stringWithFormat] are documented here:

[To see links please register here]


BOOL/bool/boolean are not even mentioned...
Reply

#5
In Objective-C, the `BOOL` type is just a signed char. From `<objc/objc.h>`:

typedef signed char BOOL;
#define YES (BOOL)1
#define NO (BOOL)0

So you can print them using the `%d` formatter But that will only print a `1` or a `0`, not `YES` or `NO`.

Or you can just use a string, as suggested in other answers.
Reply

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

NSLog(@"%d", myBool);

Reply

#7
I created a category of NSString with this

+ (instancetype)stringWithBool:(BOOL)boolValue {
return boolValue ? @"YES" : @"NO";
}

And use it like this:

[NSString stringWithBool:boolValue];
Reply

#8
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
}
Reply

#9
Add this inline function to your `.h` file:

static inline NSString* NSStringFromBOOL(BOOL aBool) {
return aBool? @"YES" : @"NO";
}

Now you are ready to go...

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

#10
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.).
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through