gpt4 book ai didi

ios - 如何在 iOS 中从 block 返回值

转载 作者:行者123 更新时间:2023-11-29 02:17:46 25 4
gpt4 key购买 nike

我正在使用 google geocode Svc 从地址获取 Lat 和 Lng,有时 Google geocode Svc 会失败(因为每秒或每天的计数太多),所以我想使用 Apple 提供的默认 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);
}
}];

关于ios - 如何在 iOS 中从 block 返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28556161/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com