Objective-C的 - 如何获得Boleans的总和? [英] Objective-C - How to get sum of Boleans?

查看:189
本文介绍了Objective-C的 - 如何获得Boleans的总和?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过code以下,我可以得到一个输出,如:
结果 0
结果
     1
结果
     1

我要的是输出这些布尔值的总和,在我的情况下,结果将是:结果 2 ,因为我们有 0 + 1 + 1

在code [更新]:


   - (无效){标记
        CLLocationCoordinate2D TG = CLLocationCoordinate2DMake(location.latitude,location.longitude);
        GMSCoordinateBounds *测试= [[GMSCoordinateBounds页头] initWithPath:路径];
        BOOL TEST3 = [测试containsCoordinate:TG];
        {
            如果(TEST3 == 1)
            {
  marker.position = CLLocationCoordinate2DMake(location.latitude,location.longitude);
            }否则如果(TEST3 == 0)
            {
  marker.position = CLLocationCoordinate2DMake(0,0);
            }
        }
    }
}



解决方案

而不是总和布尔值将,这是违反直觉的,遍历无论你正在使用以获得布尔值,如果你是,增加的计数器。这将是有YESS的数目

如果您有布尔值将数组,你可以只用过滤一个predicate阵列来获得YES值,所得数组的长度是你有YESS的数量。

编辑补充如下OP评论code样品

递增计数器

  NSUInteger numberOfBools = 0;
CLLocationCoordinate2D TG = CLLocationCoordinate2DMake(location.latitude,location.longitude);
GMSCoordinateBounds *测试= [[GMSCoordinateBounds页头] initWithPath:路径];
如果([测试containsCoordinate:TG1]){++ numberOfBools; }
如果([测试containsCoordinate:TG2]){++ numberOfBools:}
在这里... //其他试验;// numberOfBools现在包含的合格测试的数量。

编辑再次加满code后

  //剪断code以上在这里
//这是添加计数器,它初始化为0
NSUInteger numberOfBools = 0;
对于(*的NSDictionary数组字典)
{
    //剪断更多code这一点
    BOOL TEST3 = [测试containsCoordinate:TG];
    {
        如果(TEST3)
        {
        //这是你增加计数器
        ++ numberOfBools;//剪断更多code到的结束块
}//现在numberOfBools显示有多少事情过去了TEST3

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);
            }
        }
    }       
}

解决方案

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

这篇关于Objective-C的 - 如何获得Boleans的总和?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆