Objective-C,如何计算TRUE布尔数?

时间:2014-03-07 09:37:17

标签: ios objective-c

通过下面的代码,我可以获得如下输出:
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 关于如何解决它的任何想法? 附:解释这个想法的代码片段将非常有用。

3 个答案:

答案 0 :(得分:0)

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

假设,问题

让我们说

int i = 0;

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

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

答案 1 :(得分:0)

以简单的方式理解:

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 :(得分:0)

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

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

                /*...*/
            }
        }