Objective-C - How to get sum of Boleans?

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

提交回复
热议问题