Objective-C - How to get sum of Boleans?

后端 未结 4 1316
迷失自我
迷失自我 2021-01-29 06:58

Through the code below, I can get an output such as :
0
1
1

What I want is to output the sum of these boolea

4条回答
  •  长发绾君心
    2021-01-29 07:16

    int sum = (test3 ? 1 : 0) + (testX ? 1 : 0) + (testY ? 1 : 0);
    

    And not so weird variant:

    #define BOOL_TO_INT(val) ((val) ? 1 : 0)
    
    int sum = BOOL_TO_INT(test3) + BOOL_TO_INT(testX) + BOOL_TO_INT(testY);
    

提交回复
热议问题