可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
Through the code below, I can get an output such as :
0
1
1
What I want is to output the sum of these booleans values, in my case the result will be :
2
because we have 0+1+1
The code [Update] :
-(void)markers{ CLLocationCoordinate2D tg = CLLocationCoordinate2DMake(location.latitude, location.longitude); GMSCoordinateBounds *test = [[GMSCoordinateBounds alloc]initWithPath:path]; BOOL test3 = [test containsCoordinate:tg]; { if (test3 == 1) { marker.position = CLLocationCoordinate2DMake(location.latitude, location.longitude); }else if (test3 == 0) { marker.position = CLLocationCoordinate2DMake(0, 0); } } } }
回答1:
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
回答2:
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);
回答3:
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.
回答4:
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;