Search code examples
objective-cbooleanobjective-c-literals

What is the difference between @YES/@NO and YES/NO?


In Objective-c what is the difference between @YES/@NO and YES/NO? What types are used for each?


Solution

  • @YES is a short form of [NSNumber numberWithBool:YES]

    &

    @NO is a short form of [NSNumber numberWithBool:NO]

    and if we write

    if(@NO)
       some statement;
    

    the above if statement will execute since the above statement will be

    if([NSNumber numberWithBool:NO] != nil)
    

    and it's not equal to nil so it will be true and thus will pass.

    Whereas YES and NO are simply BOOL's and they are defined as-

    #define YES             (BOOL)1
    
    #define NO              (BOOL)0
    

    YES & NO is same as true & false, 1 & 0 respectively and you can use 1 & 0 instead of YES & NO, but as far as readability is concerned YES & NO will(should) be definitely preferred.