代码之家  ›  专栏  ›  技术社区  ›  Kevlar

在objective-c中,是/否、对/错和对/错之间有区别吗?

  •  142
  • Kevlar  · 技术社区  · 15 年前

    1 无/假/假均定义为 0

    9 回复  |  直到 13 年前
        1
  •  101
  •   Cœur N0mi    7 年前

    我相信有 bool BOOL
    http://iosdevelopertips.com/objective-c/of-bool-and-yes.html

    布尔 是一个 unsigned char 布尔 可以包含除 YES NO

    考虑这个代码:

    BOOL b = 42;
    
    if (b) {
        printf("b is not NO!\n");
    }
    
    if (b != YES) {
        printf("b is not YES!\n");
    }
    

    输出为:

    b不是不!

    对于大多数人来说,这是一个不必要的问题,但是如果你真的想要一个布尔值,最好使用 布尔 . 我应该补充一点:iOS SDK通常使用 布尔 .

        2
  •  84
  •   L. Cornelius Dol    10 年前

    没有实际的区别 你用 BOOL

    if(someVar ) { ... }
    if(!someVar) { ... }
    

    意思与

    if(someVar!=0) { ... }
    if(someVar==0) { ... }
    

    这就是为什么您可以将任何基元类型或表达式计算为布尔测试(包括,例如指针)。注意你应该做前者,而不是后者。

    请注意 如果将钝角值指定给所谓的 布尔 #define

    重要的是,永远不要使用角色比较测试布尔人——这不仅有风险,因为 someVar 可以指定一个非零值,该值不是“是”,但我认为更重要的是,它无法正确表达意图:

    if(someVar==YES) { ... } // don't do this!
    if(someVar==NO ) { ... } // don't do this either!
    

    换句话说,按照预期和文档记录的方式使用构造,这样你就不会在C语言中受到伤害。

        3
  •  55
  •   Ky -    9 年前

    //These will all print "1"
    NSLog(@"%d", true == true);
    NSLog(@"%d", TRUE == true);
    NSLog(@"%d", YES  == true);
    NSLog(@"%d", true == TRUE);
    NSLog(@"%d", TRUE == TRUE);
    NSLog(@"%d", YES  == TRUE);
    NSLog(@"%d", true == YES);
    NSLog(@"%d", TRUE == YES);
    NSLog(@"%d", YES  == YES);
    
    NSLog(@"%d", false == false);
    NSLog(@"%d", FALSE == false);
    NSLog(@"%d", NO    == false);
    NSLog(@"%d", false == FALSE);
    NSLog(@"%d", FALSE == FALSE);
    NSLog(@"%d", NO    == FALSE);
    NSLog(@"%d", false == NO);
    NSLog(@"%d", FALSE == NO);
    NSLog(@"%d", NO    == NO);
    
    
    //These will all print "0"
    NSLog(@"%d", false == true);
    NSLog(@"%d", FALSE == true);
    NSLog(@"%d", NO    == true);
    NSLog(@"%d", false == TRUE);
    NSLog(@"%d", FALSE == TRUE);
    NSLog(@"%d", NO    == TRUE);
    NSLog(@"%d", false == YES);
    NSLog(@"%d", FALSE == YES);
    NSLog(@"%d", NO    == YES);
    
    NSLog(@"%d", true == false);
    NSLog(@"%d", TRUE == false);
    NSLog(@"%d", YES  == false);
    NSLog(@"%d", true == FALSE);
    NSLog(@"%d", TRUE == FALSE);
    NSLog(@"%d", YES  == FALSE);
    NSLog(@"%d", true == NO);
    NSLog(@"%d", TRUE == NO);
    NSLog(@"%d", YES  == NO);
    

    输出为:

    2013-02-19 20:30:37.061 BooleanTests[27433:a0f] 1
    2013-02-19 20:30:37.061 BooleanTests[27433:a0f] 1
    2013-02-19 20:30:37.072 BooleanTests[27433:a0f] 1
    2013-02-19 20:30:37.073 BooleanTests[27433:a0f] 1
    2013-02-19 20:30:37.073 BooleanTests[27433:a0f] 1
    2013-02-19 20:30:37.074 BooleanTests[27433:a0f] 1
    2013-02-19 20:30:37.074 BooleanTests[27433:a0f] 1
    2013-02-19 20:30:37.075 BooleanTests[27433:a0f] 1
    2013-02-19 20:30:37.075 BooleanTests[27433:a0f] 1
    2013-02-19 20:30:37.076 BooleanTests[27433:a0f] 1
    2013-02-19 20:30:37.077 BooleanTests[27433:a0f] 1
    2013-02-19 20:30:37.077 BooleanTests[27433:a0f] 1
    2013-02-19 20:30:37.078 BooleanTests[27433:a0f] 1
    2013-02-19 20:30:37.078 BooleanTests[27433:a0f] 1
    2013-02-19 20:30:37.079 BooleanTests[27433:a0f] 1
    2013-02-19 20:30:37.079 BooleanTests[27433:a0f] 1
    2013-02-19 20:30:37.080 BooleanTests[27433:a0f] 1
    2013-02-19 20:30:37.080 BooleanTests[27433:a0f] 1
    2013-02-19 20:30:37.081 BooleanTests[27433:a0f] 0
    2013-02-19 20:30:37.081 BooleanTests[27433:a0f] 0
    2013-02-19 20:30:37.082 BooleanTests[27433:a0f] 0
    2013-02-19 20:30:37.091 BooleanTests[27433:a0f] 0
    2013-02-19 20:30:37.092 BooleanTests[27433:a0f] 0
    2013-02-19 20:30:37.093 BooleanTests[27433:a0f] 0
    2013-02-19 20:30:37.093 BooleanTests[27433:a0f] 0
    2013-02-19 20:30:37.094 BooleanTests[27433:a0f] 0
    2013-02-19 20:30:37.094 BooleanTests[27433:a0f] 0
    2013-02-19 20:30:37.095 BooleanTests[27433:a0f] 0
    2013-02-19 20:30:37.095 BooleanTests[27433:a0f] 0
    2013-02-19 20:30:37.096 BooleanTests[27433:a0f] 0
    2013-02-19 20:30:37.096 BooleanTests[27433:a0f] 0
    2013-02-19 20:30:37.097 BooleanTests[27433:a0f] 0
    2013-02-19 20:30:37.098 BooleanTests[27433:a0f] 0
    2013-02-19 20:30:37.101 BooleanTests[27433:a0f] 0
    2013-02-19 20:30:37.102 BooleanTests[27433:a0f] 0
    2013-02-19 20:30:37.102 BooleanTests[27433:a0f] 0
    
        4
  •  14
  •   Community c0D3l0g1c    7 年前

    你可能想看看这个问题的答案 question

    typedef signed char        BOOL; 
    // BOOL is explicitly signed so @encode(BOOL) == "c" rather than "C" 
    // even if -funsigned-char is used.
    #define OBJC_BOOL_DEFINED
    
    
    #define YES             (BOOL)1
    #define NO              (BOOL)0
    
        5
  •  13
  •   malex    5 年前

    两者之间的主要(危险!)区别 true YES 在JSON序列化中。

    例如,我们有JSON类型的服务器请求,需要在JSON sence中发送true/false:

    NSDictionary *r1 = @{@"bool" : @(true)};
    NSDictionary *r2 = @{@"bool" : @(YES)};
    NSDictionary *r3 = @{@"bool" : @((BOOL)true)};
    

    然后我们将其转换为JSON字符串,然后作为

    NSData *data = [NSJSONSerialization  dataWithJSONObject:requestParams options:0 error:nil];
    NSString *jsonString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    

    jsonString1 // {"bool":1}
    jsonString2 // {"bool":true}
    jsonString3 // {"bool":true}
    

    由于API逻辑 jsonString1 可能会导致错误。

    总而言之,只有准确 @YES 并将价值铸造为 @((BOOL)expression) 属于 __NSCFBoolean 符合事实的 使用JSON序列化。 @(expression1 && expression2) @(YES && YES) )属于 __NSCFNumber (int) 输入并转换为 1 在JSON中。

    另外,您可以简单地使用字符串值布尔值

    @{@"bool" : @"true"}; // in JSON {"bool":true}
    
        6
  •  1
  •   Grady Player    9 年前

    int i = 2;
    if(i);        //true
    if(i==YES);   // false
    if((!!i)==YES); //true
    

    所以这里的问题就是 (YES==1) 在C语言中,比较不是布尔的,而是基于值的。

    YES 只是一个 #define (而不是语言固有的东西),它必须是某种价值,并且 1 最有意义。

        7
  •  0
  •   Qix - MONICA WAS MISTREATED    9 年前

    [button setHidden:YES];
    

    听起来比

    [button setHidden:TRUE];
    
        8
  •  -3
  •   Dmytro    6 年前

    我们可以在lambda演算中构造一个名为if a then b else c的结构,如下所示:

    (\ifThenElse. <use if then else>)(\a. \b. \c. a b c)
    

    在JavaScript中,如下所示:

    (function(ifThenElse) {
        // use ifThenElse
    })(function(a) {
        return function(b) {
            return function(c) {
                return a(b)(c);
            };
        };
    });
    

    我们可以如下定义这些函数:

    (\true. <use true>)(\a. \b. a) and (\false. <use false>)(\a. \b. b)
    

    在JavaScript中,它如下所示:

    (function(True) {
        // use True
    })(function(a) {
         return function(b) {
             return a;
         }
    });
    
    (function(False) {
        // use True
    })(function(a) {
         return function(b) {
             return b;
         }
    });
    

    现在我们可以做以下事情

    (\true. \false. \ifThenElse. \doThis. \doThat. ifThenElse true doThis doThat)
    (\a. \b. a)(\a. \b. b)(\a. \b. \c. a b c)(\a. ())(\a. ())
    

    让我们看看这一点。

    (function(True) {
        return (function(False) {
            return (function(ifThenElse) {
                return (function(doThis) {
                    return (function(doThat) {
                        return ifThenElse(True)(doThis)(doThat);
                    });
                });
            });
        })
    })(function(a) {
         return function(b) {
             return a;
         }
    })(function(a) {
         return function(b) {
             return b;
         }
    })(function(a) {
        return function(b) {
            return function(c) {
                return a(b)(c);
            };
        };
    })(function(a) { console.log("you chose LEFT!"); })
    (function(a) {console.log("you chose RIGHT");})();
    

    总而言之

    function ChooseRight(left) {
        return function _ChooseRight_inner(right) {
            return right;
        }
    }
    function ChooseLeft(left) {
        return function _ChooseLeft_inner(right) {
            return left;
        }
    }
    
    var env = {
        '0': ChooseLeft,
        '1': ChooseRight,
        'false': ChooseRight,
        'true': ChooseLeft,
        'no': ChooseRight
        'yes': ChooseLeft,
        'snd': ChooseRight,
        'fst': ChooseLeft
    };
    var _0 = env['0'];
    var _1 = env['1'];
    var _true = env['true'];
    var _false = env['false'];
    var yes = env['yes'];
    var no = env['no'];
    
    // encodes church zero or one to boolean
    function lambda_encodeBoolean(self) {
        return self(false)(true);
    }
    // decodes a Boolean to church zero or one
    function lambda_decodeBoolean(self) {
        console.log(self, self ? env['true'] : env['false']);
        return self ? env['true'] : env['false'];
    }
    
    lambda_decodeBoolean('one' === 'two')(function() {
        console.log('one is two');
    })(function() {
        console.log('one is not two');
    })();
    
    lambda_decodeBoolean('one' === 'one')(function() {
        console.log('one is one');
    })(function() {
        console.log('one is not one');
    })();
    
        9
  •  -7
  •   Qix - MONICA WAS MISTREATED    9 年前

    否,是/否是表示真/假(1/0)的另一种方式