Objective-C - How to get sum of Boleans?

后端 未结 4 1208
迷失自我
迷失自我 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:12

    Rather than sum BOOLs, which is counterintuitive, loop over whatever you are using to get the BOOL values, and if you get YES, increment a counter. This will be the number of YESs that you have.

    If you have an array of BOOLs, you could just filter the array with a predicate to get the YES values and the length of the resulting array is the number of YESs that you have.

    Edited to add code samples following OP comments

    Incrementing a counter

    NSUInteger numberOfBools = 0;
    CLLocationCoordinate2D tg = CLLocationCoordinate2DMake(location.latitude, location.longitude);
    GMSCoordinateBounds *test = [[GMSCoordinateBounds alloc]initWithPath:path];
    if ([test containsCoordinate:tg1]) { ++numberOfBools; }
    if ([test containsCoordinate:tg2]) { ++numberOfBools: }
    ... // other tests here;
    
    // numberOfBools now contains the number of passing tests.
    

    Edited Again, after the full code was added

    // snipped code above here
    // This is where you add the counter and initialise it to 0
    NSUInteger numberOfBools = 0;
    for (NSDictionary *dictionary in array)
    {            
        // Snip more code to this point
        BOOL test3 = [test containsCoordinate:tg];
        {
            if (test3)
            {
            // This is where you increment the counter
            ++numberOfBools;
    
    // Snip more code to the end of the for block
    }
    
    // Now numberOfBools shows how many things passed test3
    
    0 讨论(0)
  • 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);
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-01-29 07:23

    Another way to do this is to assume that if any one value is false, then the entire array is false, so, loop over the array until a false value is found, then break:

    BOOL retval = true;  //return holder variable
    
    /*'boolsNumArray' is an NSArray of NSNumber instances, converted from BOOLs:
    
        //BOOL-to-NSNumber conversion (makes 'boolsNumArray' NSArray, below)!
        '[myNSArray addObject:[NSNumber numberWithBool:yourNextBOOL]]' 
    
    */
    for(NSNumber* nextNum in boolsNumArray) {
        if([nextNum boolValue] == false) {
            retval = false;
            break;
        }
    }
    
    return retval;
    
    0 讨论(0)
提交回复
热议问题