Objective-C - How to get sum of Boleans?

后端 未结 4 1210
迷失自我
迷失自我 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条回答
  •  闹比i
    闹比i (楼主)
    2021-01-29 07:17

    You can just add BOOLs since bools are just integers. e.g. :

    int sum = 0;
    
    CLLocationCoordinate2D tg = CLLocationCoordinate2DMake(location.latitude, location.longitude);
    GMSCoordinateBounds *test = [[GMSCoordinateBounds alloc]initWithPath:path];
    BOOL test3 = [test containsCoordinate:tg];
    //Obtain bolean values :
    
    BOOL testX = /* other test */;
    BOOL testY = /* other test */;
    
    sum = test3 + testX + testY
    

    This is a bad idea however, as BOOLs aren't necessarily 1 or 0. They are 0 and not 0

    BOOL is just a typedef-ed char: typedef signed char BOOL; YES and NO are 1 and 0, but BOOL variable = 2 is perfectly valid

    For example:

    - (int) testX
    {
        if(inState1) return 1;
        if(inState2) return 2;
    
        else return 0;
    }
    
    BOOL textXResult = [self testX]; //Might return 2, this is still equivalent to YES.
    

    The best solution is to iterate your BOOLs and instead count the number of YESes.

提交回复
热议问题