gpt4 book ai didi

objective-c - 我的应用程序在尝试获取包含特殊字符的字符串时崩溃

转载 作者:行者123 更新时间:2023-11-29 04:49:27 25 4
gpt4 key购买 nike

当我尝试显示字符串时,我的应用程序崩溃了:

NSLog(@"The start point is: %@",startPt);

这是 startPt 值:39-49%20Avenue%20d'Ivry,%2075013%20Paris,%20France。实际上它是从 Google map 网络服务返回的。

有时,应用程序没有崩溃,但我在日志中发现了一些奇怪的东西:

The start point is: text/javascript

为什么我无法将字符串 39-49%20Avenue%20d'Ivry,%2075013%20Paris,%20France 分配给 NSString 变量?

编辑:

这是 JSON 响应解析:

NSString *responseString=[request responseString];
NSDictionary *responseDict=[responseString JSONValue];

NSArray *resultsArray = [responseDict objectForKey:@"results"];

location=[[resultsArray valueForKey:@"formatted_address"] objectAtIndex:0];
location=[location stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
NSLog(@"The location is : %@",location);//39-49%20Avenue%20d'Ivry,%2075013%20Paris,%20France

这是从 Google map API 返回的 JSON 响应:

{
"results" : [
{
"address_components" : [
{
"long_name" : "39-49",
"short_name" : "39-49",
"types" : [ "street_number" ]
},
{
"long_name" : "Avenue d'Ivry",
"short_name" : "Avenue d'Ivry",
"types" : [ "route" ]
},
{
"long_name" : "Quartier de la Gare",
"short_name" : "Quartier de la Gare",
"types" : [ "neighborhood", "political" ]
},
{
"long_name" : "13th arrondissement of Paris",
"short_name" : "13th arrondissement of Paris",
"types" : [ "sublocality", "political" ]
},
{
"long_name" : "Paris",
"short_name" : "Paris",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Paris",
"short_name" : "75",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Île-de-France",
"short_name" : "IdF",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "France",
"short_name" : "FR",
"types" : [ "country", "political" ]
},
{
"long_name" : "75013",
"short_name" : "75013",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "39-49 Avenue d'Ivry, 75013 Paris, France",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 48.82332110,
"lng" : 2.36638740
},
"southwest" : {
"lat" : 48.82296810,
"lng" : 2.36571090
}
},
"location" : {
"lat" : 48.82313960,
"lng" : 2.36602210
},
"location_type" : "RANGE_INTERPOLATED",
"viewport" : {
"northeast" : {
"lat" : 48.82449358029150,
"lng" : 2.367398130291502
},
"southwest" : {
"lat" : 48.82179561970850,
"lng" : 2.364700169708498
}
}
},
"types" : [ "street_address" ]
},
{
"address_components" : [
{
"long_name" : "Quartier de la Gare",
"short_name" : "Quartier de la Gare",
"types" : [ "neighborhood", "political" ]
},
{
"long_name" : "13th arrondissement of Paris",
"short_name" : "13th arrondissement of Paris",
"types" : [ "sublocality", "political" ]
},
{
"long_name" : "Paris",
"short_name" : "Paris",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Paris",
"short_name" : "75",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Île-de-France",
"short_name" : "IdF",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "France",
"short_name" : "FR",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Quartier de la Gare, Paris, France",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 48.83810700000001,
"lng" : 2.3869610
},
"southwest" : {
"lat" : 48.8189240,
"lng" : 2.3556050
}
},
"location" : {
"lat" : 48.82807520,
"lng" : 2.36740680
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 48.83810700000001,
"lng" : 2.3869610
},
"southwest" : {
"lat" : 48.8189240,
"lng" : 2.3556050
}
}
},
"types" : [ "neighborhood", "political" ]
},
{
"address_components" : [
{
"long_name" : "13th arrondissement of Paris",
"short_name" : "13th arrondissement of Paris",
"types" : [ "sublocality", "political" ]
},
{
"long_name" : "Paris",
"short_name" : "Paris",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Paris",
"short_name" : "75",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Île-de-France",
"short_name" : "IdF",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "France",
"short_name" : "FR",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "13th arrondissement of Paris, Paris, France",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 48.84496499999999,
"lng" : 2.3900530
},
"southwest" : {
"lat" : 48.8155730,
"lng" : 2.3411080
}
},
"location" : {
"lat" : 48.83222220,
"lng" : 2.35555560
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 48.84496499999999,
"lng" : 2.3900530
},
"southwest" : {
"lat" : 48.8155730,
"lng" : 2.3411080
}
}
},
"types" : [ "sublocality", "political" ]
},
{
"address_components" : [
{
"long_name" : "75013",
"short_name" : "75013",
"types" : [ "postal_code" ]
},
{
"long_name" : "Paris",
"short_name" : "Paris",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Paris",
"short_name" : "75",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Île-de-France",
"short_name" : "IdF",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "France",
"short_name" : "FR",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "75013 Paris, France",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 48.84493089999999,
"lng" : 2.39026440
},
"southwest" : {
"lat" : 48.81587960,
"lng" : 2.34112720
}
},
"location" : {
"lat" : 48.8307590,
"lng" : 2.3592040
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 48.84493089999999,
"lng" : 2.39026440
},
"southwest" : {
"lat" : 48.81587960,
"lng" : 2.34112720
}
}
},
"types" : [ "postal_code" ]
},
{
"address_components" : [
{
"long_name" : "Paris",
"short_name" : "Paris",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Paris",
"short_name" : "75",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Île-de-France",
"short_name" : "IdF",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "France",
"short_name" : "FR",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Paris, France",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 48.9021450,
"lng" : 2.46992090
},
"southwest" : {
"lat" : 48.8155730,
"lng" : 2.2241990
}
},
"location" : {
"lat" : 48.8566140,
"lng" : 2.35222190
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 48.9021450,
"lng" : 2.46992090
},
"southwest" : {
"lat" : 48.8155730,
"lng" : 2.2241990
}
}
},
"types" : [ "locality", "political" ]
},
{
"address_components" : [
{
"long_name" : "Paris",
"short_name" : "75",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Île-de-France",
"short_name" : "IdF",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "France",
"short_name" : "FR",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Paris, France",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 48.9021450,
"lng" : 2.46992090
},
"southwest" : {
"lat" : 48.8155730,
"lng" : 2.2241990
}
},
"location" : {
"lat" : 48.87633370,
"lng" : 2.34612070
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 48.9021450,
"lng" : 2.46992090
},
"southwest" : {
"lat" : 48.8155730,
"lng" : 2.2241990
}
}
},
"types" : [ "administrative_area_level_2", "political" ]
},
{
"address_components" : [
{
"long_name" : "Île-de-France",
"short_name" : "IdF",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "France",
"short_name" : "FR",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Île-de-France, France",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 49.2415040,
"lng" : 3.5590070
},
"southwest" : {
"lat" : 48.1200810,
"lng" : 1.446170
}
},
"location" : {
"lat" : 48.84991980,
"lng" : 2.63704110
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 49.2415040,
"lng" : 3.5590070
},
"southwest" : {
"lat" : 48.1200810,
"lng" : 1.446170
}
}
},
"types" : [ "administrative_area_level_1", "political" ]
},
{
"address_components" : [
{
"long_name" : "France",
"short_name" : "FR",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "France",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 51.08896180,
"lng" : 9.559956999999999
},
"southwest" : {
"lat" : 41.34232760,
"lng" : -5.1412280
}
},
"location" : {
"lat" : 46.2276380,
"lng" : 2.2137490
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 51.08896180,
"lng" : 9.559956999999999
},
"southwest" : {
"lat" : 41.34232760,
"lng" : -5.1412280
}
}
},
"types" : [ "country", "political" ]
}
],
"status" : "OK"
}

最佳答案

我能想到两种可能性:


(1) 为什么假设字符串是 ASCII?尝试不同的编码:

location = [location stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

也许 Google 使用的是 unicode ',而不是 ASCII 集中的Unicode?如果您正在查看地址数据,那么您可以相当确定其中会放入奇怪的字符!


和 (2) (考虑到您已在问题中发布了 JSON,但这个可能性较小,但值得考虑)

当您第一次获取位置时,您不会检查它实际上是一个字符串(您也不会检查“formatted_address”是否是一个数组!)

如果返回的 JSON 是这样的(假设服务器中有错误?)怎么办:

{ 
...
"formatted_address" : [ { } ]
...
}

在这里,[[resultsArray valueForKey:@"formatted_address"] objectAtIndex:0]; 可以工作,但它不会返回 NSString

尝试添加这种检查:

location=[[resultsArray valueForKey:@"formatted_address"] objectAtIndex:0];
if (NO == [location isKindOfClass:[NSString class]]) {
NSLog(@"Location wasn't a string (%@) : %@", [location class], location);
} else {
location=[location stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
NSLog(@"The location is : %@",location);//39-49%20Avenue%20d'Ivry,%2075013%20Paris,%20France
}

关于objective-c - 我的应用程序在尝试获取包含特殊字符的字符串时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9114710/

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