-1 arrays boolean objective-c ios
通过下面的代码,我可以获得如下输出:
0
1
1
我想要的是输出这些布尔值的总和,在我的例子中,结果将是:
2因为我们有0+1+1
代码[更新]:
Run Code Online (Sandbox Code Playgroud)-(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); } } } }
而不是求和BOOLs,这是违反直觉的,循环使用你用于获取BOOL值的任何东西,如果你得到YES,则增加一个计数器.这将是您拥有的YES数.
如果你有一个BOOL数组,你可以只使用谓词过滤数组以获得YES值,结果数组的长度是你拥有的YES数.
编辑后在OP注释后添加代码示例
递增一个柜台
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.
Run Code Online (Sandbox Code Playgroud)
再次编辑,添加完整代码后
// 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
Run Code Online (Sandbox Code Playgroud)