gpt4 book ai didi

ios - 循环应该运行 4 次,但只执行一次

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

我正在制作一个基于地理定位的应用程序。这是我的代码:

-(void)gettingDataSQL {
NSURL *url = [NSURL URLWithString:@"http://www.xxx.fr/xxx/xxx/script_xxx_localisation.php"];
NSData *data = [NSData dataWithContentsOfURL:url];

if (data != nil)
{
jsonArray = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];

NSLog(@"jSonArrayCount = %i", [jsonArray count]);

for (int i = 0; i<jsonArray.count; i++) {

NSString *address = [[jsonArray objectAtIndex:i] objectForKey:@"adresse"];
NSLog(@"address FROM JSON = %@", address);
NSString *titre = [[jsonArray objectAtIndex:i] objectForKey:@"titre"];
NSString *stringId = [[jsonArray objectAtIndex:i] objectForKey:@"id"];

[geocoder geocodeAddressString:address completionHandler:^(NSArray* placemarks, NSError* error){

// Check for returned placemarks
if (placemarks && placemarks.count > 0) {
NSLog(@"if placemarks && placemarks.count");
CLPlacemark *topResult = [placemarks objectAtIndex:0];

MKPlacemark *placemark = [[MKPlacemark alloc]initWithPlacemark:topResult];
// [self.mapViewOutlet addAnnotation:placemark];

NSLog(@"Placemark posé au longitude : %f et latitude : %f", placemark.location.coordinate.longitude, placemark.location.coordinate.latitude);

MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
annotation.coordinate = placemark.location.coordinate;
annotation.title = titre;
annotation.subtitle = stringId;

[mapViewOutlet addAnnotation:annotation];


}
}];


NSLog(@"one placemark well defined via retrieveData:");
}
}
}

根据我所做的所有 NSLog,我了解到 for int 循环按预期被调用了 4 次,但是循环中的整个代码都没有被调用。

这部分循环只被调用一次,我不明白为什么:

[geocoder geocodeAddressString:address completionHandler:^(NSArray* placemarks, NSError* error){

// Check for returned placemarks
if (placemarks && placemarks.count > 0) {
NSLog(@"if placemarks && placemarks.count");
CLPlacemark *topResult = [placemarks objectAtIndex:0];

MKPlacemark *placemark = [[MKPlacemark alloc]initWithPlacemark:topResult];
// [self.mapViewOutlet addAnnotation:placemark];

NSLog(@"Placemark posé au longitude : %f et latitude : %f", placemark.location.coordinate.longitude, placemark.location.coordinate.latitude);

MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
annotation.coordinate = placemark.location.coordinate;
annotation.title = titre;
annotation.subtitle = stringId;

[mapViewOutlet addAnnotation:annotation];


}
}];

我想在 map 上创建多个注释 View ,而不是一个。我在数据库中有多个条目已下载,以便在 JsonArray 循环中的 map 上显示,但地理编码后的代码仅调用一次,因此它仅显示我的数据库的第一个条目。

有什么想法吗?

在此先感谢您的帮助和阅读;)我希望它能帮助其他遇到同样问题的人!

回答后

JSON 数组的日志:

2014-09-15 21:24:21.544 xxxx[1416:60b] (
{
adresse = "115 rue de reuilly";
cree = "2014-07-13 21:03:23";
description = "Venez boire!";
icone = "icone_base.png";
id = 1;
"id_annonceur" = 1;
"id_type" = 3;
lat = "";
lon = "";
note = 0;
portee = 25;
titre = "La premiere Leffe gratuite!";
validite = "";
vues = 0;
},
{
adresse = "107 rue de reuilly";
cree = "2014-07-13 22:21:24";
description = "Le Match n'est pas fini, profitez-en !";
icone = "icone_base.png";
id = 2;
"id_annonceur" = 1;
"id_type" = 3;
lat = "";
lon = "";
note = 0;
portee = 25;
titre = "Karhu a -50%";
validite = "";
vues = 0;
},
{
adresse = "Metropole de la Gare";
cree = "2014-07-19 15:57:51";
description = "Premier arrive, premier servi!";
icone = "icone_base.png";
id = 4;
"id_annonceur" = 1;
"id_type" = 1;
lat = "";
lon = "";
note = 0;
portee = 25;
titre = "Le Mojito est gratuit!";
validite = "";
vues = 0;
},
{
adresse = "2 rue de reuilly";
cree = "2014-07-19 15:59:12";
description = "Depechez-vous !";
icone = "icone_base.png";
id = 5;
"id_annonceur" = 1;
"id_type" = 1;
lat = "";
lon = "";
note = 0;
portee = 25;
titre = "La dose du Martini doublee pour 1h!";
validite = "";
vues = 0;
}

最佳答案

“发起正向地理编码请求后,请勿尝试发起另一个正向或反向地理编码请求。”- 引用自 Apple 文档

https://developer.apple.com/Library/ios/documentation/CoreLocation/Reference/CLGeocoder_class/Reference/Reference.html#//apple_ref/occ/instm/CLGeocoder/geocodeAddressString:completionHandler :

关于ios - 循环应该运行 4 次,但只执行一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25851705/

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