【问题标题】:Objective-C, How to count the number of TRUE booleans? [duplicate]Objective-C,如何计算 TRUE 布尔值的数量? [复制]
【发布时间】:2014-03-07 09:37:16
【问题描述】:

通过下面的代码,我可以获得如下输出:
0
1
1

这一行正在生成来自外部数据库的布尔值(您可以在下面的完整代码中查看它):
BOOL test3 = [test containsCoordinate:tg];

问题:

我想计算前面所说的 BOOL 行生成的 TRUE 布尔值的数量;例如输出,执行该行时找到了多少1

代码:

  -(void)markers{
NSURL *url = [NSURL URLWithString:@"http://example.com/api/s.php"];
data = [NSData dataWithContentsOfURL:url];   
NSError *error;
NSMutableArray *array = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error];
if (error != nil)
{
    NSLog(@"Could not fetch data !");
}    
NSURL *jsonURL = [NSURL URLWithString:[NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/directions/json?origin=%@&destination=%@&sensor=false", coordi, f_query]];
NSData *jsonData = [NSData dataWithContentsOfURL:jsonURL];
NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];      
id array2 = [dataDictionary objectForKey:@"routes"];
if([array2 count] > 0) {
    NSDictionary *routes = [dataDictionary objectForKey:@"routes"][0];
    NSDictionary *route = [routes objectForKey:@"overview_polyline"];
    NSDictionary *legs = [routes objectForKey:@"legs"][0];
    NSString *overview_route = [route objectForKey:@"points"];
    CLLocationCoordinate2D location;
    for (NSDictionary *dictionary in array)
    {            
        location.latitude = [dictionary[@"latitude"] floatValue];
        location.longitude = [dictionary[@"longitude"] floatValue];
        CLLocationCoordinate2D tg = CLLocationCoordinate2DMake(location.latitude, location.longitude);
        GMSCoordinateBounds *test = [[GMSCoordinateBounds alloc]initWithPath:path];
        BOOL test3 = [test containsCoordinate:tg];
        {
            if (test3 == 1)
            {
   polyline.strokeColor = [UIColor colorWithRed:0.259 green:0.698 blue:0.894 alpha:1.0];
  marker.position = CLLocationCoordinate2DMake(location.latitude, location.longitude);                     
            }else if (test3 == 0)
            {
  polyline.strokeColor = [UIColor colorWithRed:0.647 green:0.839 blue:0.016 alpha:0.7];
  marker.position = CLLocationCoordinate2DMake(0, 0);
            }
        }
    }       
}

我花了一整天的时间寻找如何做,但没有机会:s 关于如何解决它的任何想法? 附言解释这个想法的代码 sn-p 将非常有帮助。

【问题讨论】:

  • 为什么你没有一个'int count'并在每次test3为YES时加一
  • 不,每次在所需边界内找到坐标时,它都会输出1,增加一不会得到所需的结果。
  • 正如@Emmanuel 所说,您之前已经问过这个问题,并且有不少回复可以帮助您解决问题。重新发布本质上相同的问题似乎有点徒劳。
  • “完全不是想要的答案”?我花时间向您解释它,在您要求我之后用代码对其进行了两次编辑,并且您在这里接受了与我的略有不同的答案。谢谢你告诉我。

标签: ios objective-c


【解决方案1】:

以简单的方式理解:

int trueCount = 0;
int falseCount = 0;


for (<#initialization#>; <#condition#>; <#increment#>) {

  BOOL test3 = [test containsCoordinate:tg];

    if (test3) {

        trueCount++;
    }
    else{

        falseCount++;
    }

}

NSLog(@"True : %i",trueCount);
NSLog(@"False : %i",falseCount);

实施:

-(void)markers{
    NSURL *url = [NSURL URLWithString:@"http://example.com/api/s.php"];
    data = [NSData dataWithContentsOfURL:url];
    NSError *error;
    NSMutableArray *array = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error];
    if (error != nil)
    {
        NSLog(@"Could not fetch data !");
    }
    NSURL *jsonURL = [NSURL URLWithString:[NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/directions/json?origin=%@&destination=%@&sensor=false", coordi, f_query]];
    NSData *jsonData = [NSData dataWithContentsOfURL:jsonURL];
    NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
    id array2 = [dataDictionary objectForKey:@"routes"];
    if([array2 count] > 0) {
        NSDictionary *routes = [dataDictionary objectForKey:@"routes"][0];
        NSDictionary *route = [routes objectForKey:@"overview_polyline"];
        NSDictionary *legs = [routes objectForKey:@"legs"][0];
        NSString *overview_route = [route objectForKey:@"points"];
        CLLocationCoordinate2D location;

        int trueCount = 0;
        int falseCount = 0;


        for (NSDictionary *dictionary in array)
        {
            location.latitude = [dictionary[@"latitude"] floatValue];
            location.longitude = [dictionary[@"longitude"] floatValue];
            CLLocationCoordinate2D tg = CLLocationCoordinate2DMake(location.latitude, location.longitude);
            GMSCoordinateBounds *test = [[GMSCoordinateBounds alloc]initWithPath:path];
            BOOL test3 = [test containsCoordinate:tg];

            if (test3) {

                trueCount++;
            }
            else{

                falseCount++;
            }



            {
                if (test3 == 1)
                {
                    polyline.strokeColor = [UIColor colorWithRed:0.259 green:0.698 blue:0.894 alpha:1.0];
                    marker.position = CLLocationCoordinate2DMake(location.latitude, location.longitude);
                }else if (test3 == 0)
                {
                    polyline.strokeColor = [UIColor colorWithRed:0.647 green:0.839 blue:0.016 alpha:0.7];
                    marker.position = CLLocationCoordinate2DMake(0, 0);
                }
            }
        }

        NSLog(@"True : %i",trueCount);
        NSLog(@"False : %i",falseCount);


    }

【讨论】:

  • 你是救生员!!!非常感谢!!!!
【解决方案2】:

我想说,在你的 for 循环之前,你可以初始化一个计数器值,并在每次找到肯定结果时增加它:

- (void) marker
        {
            int numberOfHits = 0; 
            for(NSdictionnary * items in array)
            { 
                BOOL test3 = [test containsCoordinate:tg];
                numberOfHits += test3;

                /*...*/
            }
        }

【讨论】:

    【解决方案3】:

    创建一个int 变量并在您获得BOOL 变量的TRUE 值时递增它。

    假设,对于问题

    我们说

    int i = 0;
    

    然后,每当您获得 TRUE 的值时,增加 i 的值

    BOOL test3 = [test containsCoordinate:tg];
    if(test3)
    {
      ++i;
      NSLog(@"The value of i is : %d", i);
    }
    

    【讨论】:

      猜你喜欢
      • 2016-04-08
      • 1970-01-01
      • 1970-01-01
      • 2016-01-31
      • 2011-12-23
      • 2019-06-10
      • 2013-05-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多