Printing a YES or NO from a BOOL


#1

There is no easy way to print a YES or NO string from a value of type BOOL in the Objective-C Language, because a value of type BOOL is an integer number, not a character nor a sequence of characters (text).

You have, however, several options at your disposal:

  • Use the conditional operator;
  • Use a macro;
  • Use a block or
  • Use a free function or a static method.

Copy and paste into Xcode to view:

//  main.m - Print YES or NO from a BOOL

#import <Foundation/Foundation.h>

// ------------------------------------------------------
// Macros for logging
#define MY_Logs(P1) NSLog (@"%s: %s", __PRETTY_FUNCTION__, P1)
#define MY_Log1(P1, P2) NSLog (@"%s: "P1, __PRETTY_FUNCTION__, P2)

// ------------------------------------------------------
//
// Using a class method
@interface MyUtil
+ (const char*)stringFromBool:(BOOL)v;
@end

// Using a free function
const char *StringFromBool (BOOL);

// ------------------------------------------------------
#pragma mark -

int main(int argc, const char * argv[])
{
    @autoreleasepool {
        BOOL y = YES, n = NO;
        const char *yes = "YES", *no  = "NO";
        
        MY_Logs ("Using the binary operator...");
        MY_Log1 (@"(0)---> %s", y ? yes : no);
        MY_Log1 (@"(0)---> %s", n ? yes : no);
                
        MY_Logs ("Using a macro...");
#define YesOrNo(P1) (P1 ? "YES" : "NO")
        MY_Log1 (@"(0)---> %s", YesOrNo (y));
        MY_Log1 (@"(0)---> %s", YesOrNo (n));

        MY_Logs ("Using a class method or a free function...");
        MY_Log1 (@"(2)---> %s", StringFromBool (y));
        MY_Log1 (@"(2)---> %s", StringFromBool (y));
        MY_Log1 (@"(1)---> %s", [MyUtil stringFromBool:y]);
        MY_Log1 (@"(1)---> %s", [MyUtil stringFromBool:n]);
       
        MY_Logs ("Using a nameless block...");
        MY_Log1 (@"(3)---> %s", ^(BOOL v) {return v ? yes : no;} (y));
        MY_Log1 (@"(3)---> %s", ^(BOOL v) {return v ? yes : no;} (n));
        
        MY_Logs ("Using a named block - my favourite...");
        const char* (^boolToStr)(BOOL) = ^ (BOOL v) {
            return (const char*)(v ? yes : no);
        };
        
        MY_Log1 (@"(4)---> %s", boolToStr (y));
        MY_Log1 (@"(4)---> %s", boolToStr (n));
    }
    return 0;
}

// ------------------------------------------------------
#pragma mark -

@implementation MyUtil
+ (const char*)stringFromBool:(BOOL)v
{
    return StringFromBool(v);
}

const char *StringFromBool (BOOL v)
{
    const char *yes = "YES";
    const char *no  = "NO";

    return v ? yes : no;   
}
@end

#2

What about my way :slight_smile:

[code]#import <Foundation/Foundation.h>

int main(int argc, const char * argv[])
{

@autoreleasepool {
    
    // Chapter 13 challange: tell if it's currently daylight savings time
    
    // Make an instance and check what it returns
    NSTimeZone *timezone = [[NSTimeZone alloc]init];
    NSLog(@"The timezone class returns %@", timezone);
    
    // Try to get a yes or no out of it
    NSUInteger whatIsTheZone = [timezone isDaylightSavingTime];
    
    // The output is a 0 or 1, so make it no or yes
   char *YesOrNo = "Don't know";
    if (whatIsTheZone) {
         YesOrNo = "Yes it is! Indeed :)";
    }else{
        YesOrNo = "Nope, not, no.";
    }
    
    NSLog(@"Is it daylight savings time? %s", YesOrNo);
    
}
return 0;

}
[/code]