如何 return 来自 iOS 块中的值

How To return a value From block in iOS

我正在使用 google geocode Svc 从地址获取 Lat 和 Lng,有时 Google geocode Svc 会失败(因为每秒或每天的计数太多),所以我想使用 Apple 提供的 Default Geocoder Svc。

看这里我的代码

 -(void)getLatandlongfromAddress
   {
    CLLocationCoordinate2D Coordinate=[self   geoCodeUsingAddress:@"orlando"];

   }

- (CLLocationCoordinate2D)geoCodeUsingAddress:(NSString *)address
 {
   NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:req]];
        NSError *err = nil;
        NSMutableDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&err];
        if (!err)
        {
            NSLog(@"status = %@",[jsonDict objectForKey:@"status"]);
            if ([[jsonDict objectForKey:@"status"] isEqualToString:@"OK"])
            {

                  //If status is cmng Im returned lat and long  


            }
            else
           {
                 // I want to use geocode Svc


                CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString:@"Orlando 32835" completionHandler:^(NSArray *placemarks, NSError *error) {
    if (error) {
        NSLog(@"%@", error);
    } else {

        NSLog(@"%@",placemarks);
        CLPlacemark *placemark = [placemarks lastObject];

    }
}];




           }

         }


}

指导我任何想法,谢谢..

您的代码中有几个错误:

  1. 如果您调用 [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&err]; 并将 kNilOptions 作为选项参数传递,您不会得到可变字典而是不可变字典。
  2. 您不应该检查是否没有错误,而是检查您的数据是否为 ​​nil。

这是您更正后的代码(包括完成处理程序):

- (CLLocationCoordinate2D)geoCodeUsingAddress:(NSString *)address completionHandler:(void (^)(CLLocation *location, NSError *error))completionHandler {
   NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:req]];    // Whatever `req` is...
    NSError *jsonError = nil;
    NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&jsonError];
    if (jsonDict) {
        NSLog(@"status = %@", [jsonDict objectForKey:@"status"]);
        if ([[jsonDict objectForKey:@"status"] isEqualToString:@"OK"]) {
            // If status is cmng Im returned lat and long
            // Get them from you local file
            CLLocationDegrees latitude = 30.0;
            CLLocationDegrees longitude = 50.0;
            CLLocation *location = [[CLLocation alloc] initWithLatitude:latitude longitude:longitude];
            completionHandler(location, nil);
        } else {
            CLGeocoder *geocoder = [[CLGeocoder alloc] init];
            [geocoder geocodeAddressString:@"Orlando 32835" completionHandler:^(NSArray *placemarks, NSError *error) {
                if (placemarks.count) {
                    NSLog(@"%@",placemarks);
                    CLPlacemark *placemark = [placemarks lastObject];
                    completionHandler(placemark.location, nil);
                } else {
                    NSLog(@"%@", error);
                    completionHandler(nil, error);
                }
            }];
        }
    } else {
        // Populate the error
        completionHandler(nil, [NSError errorWithDomain:@"YOUR_DOMAIN" code:1000 userInfo:nil]);
    }
}

您可以像使用完成处理程序的任何其他方法一样调用它:

[self geoCodeUsingAddress:@"Orlando 32835" completionHandler:^(CLLocation *location, NSError *error) {
    if (location) {
        // Use location
    } else {
        NSLog(@"Error: %@", error.userInfo);
    }
}];