Я читаю данные JSON с сервера, и я пытаюсь разобрать определенную строку, содержащую URL-адрес HTML. Данные находятся в NSString, называемом current_weatherIconUrl2. Выход консоли от Xcode ниже:«Завершение приложения из-за неперехваченного исключения« Ошибка NSInvalidArgumentException »с помощью stringByTrimmingCharactersInSet на iPhone
2010-07-06 17: 17: 46,628 WhatToDo [13437: 20b] Текущее состояние: URL2 :("http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0001_sunny.png")
То, что я пытался сделать, это использовать "stringByTrimmingCharactersInSet" для разбора '(', '"', а также линия подачи и перевода строки, но я вместо того, чтобы получать NSInvalidArgumentException:
2010-06-30 00: 18: 07.357 WhatToDo [7299: 20b] * Завершение приложения из-за неперехваченного исключения 'NSInvalidArgumentExcept ион», причина:„* - [NSCFArray stringByTrimmingCharactersInSet]: непризнанный селектор направил к экземпляру 0x3e84970“
Вот выдержка из данных, который возвращается с сервера (информации, которую я хочу, чтобы извлечь выделен жирным) :
2010-07-06 17:17:46.627 WhatToDo[13437:20b] Results: {
data = {
"current_condition" = (
{
cloudcover = 75;
humidity = 42;
"observation_time" = "08:47 AM";
precipMM = "0.0";
pressure = 1003;
"temp_C" = 36;
"temp_F" = 97;
visibility = 10;
weatherCode = 113;
weatherDesc = (
{
value = Sunny;
}
);
**weatherIconUrl = (
{
value = "http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0001_sunny.png";
}
);**
winddir16Point = WSW;
winddirDegree = 250;
windspeedKmph = 31;
windspeedMiles = 19;
}
);
};
}
Вот код, который анализирует данные в переменных NSString.
- (void) readWeather {
NSLog(@"readWeather started");
NSMutableArray *imageArray = [[NSMutableArray alloc] init];
NSData *jsonObjectString;
NSString *completeString = [NSString stringWithFormat:
@"http://www.worldweatheronline.com/feed/weather.ashx?key=988ae47cc3062016100606&lat=25.04&lon=121.55&num_of_days=5&format=json", jsonObjectString];
NSLog(@"Weather URL: %@", completeString);
NSURL *urlForValidation = [NSURL URLWithString:completeString];
NSMutableURLRequest *validationRequest = [[NSMutableURLRequest alloc] initWithURL:urlForValidation];
[validationRequest setHTTPMethod:@"POST"];
NSData *responseData = [NSURLConnection sendSynchronousRequest:validationRequest returningResponse:nil error:nil];
[validationRequest release];
// Store incoming data into a string
NSString *jsonString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
// Create a dictionary from the JSON string
NSDictionary *results = [jsonString JSONValue];
NSLog(@"Results: %@", results);
NSArray *data = [[results objectForKey:@"data"] objectForKey:@"current_condition"];
NSArray *data2 = [[results objectForKey:@"data"] objectForKey:@"weather"];
NSCharacterSet *skipSet = [NSCharacterSet characterSetWithCharactersInString:@"("];
for (NSArray *current_condition in data)
{
// Get title of the image
NSString *temp_C = [current_condition valueForKey:@"temp_C"];
NSString *current_weatherDesc = [current_condition valueForKey:@"weatherDesc"];
NSString *current_weatherIconUrl = [current_condition valueForKey:@"weatherIconUrl"];
NSString *current_weatherIconUrl2= [current_weatherIconUrl valueForKey:@"value"];
//Commented out because this will create an exception:
//NSString *current_weatherIconUrl3 = [current_weatherIconUrl2 stringByTrimmingCharactersInSet:skipSet];
//NSLog(@"Current Condition: URL2: %@", *(current_weatherIconUrl3);
//Commented out because this will create an exception:
//current_weatherIconUrl3 = [current_weatherIconUrl3 stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}
Может кто-нибудь сказать мне, что я делаю неправильно или если есть лучший способ, чем использование stringByTrimmingCharactersInSet
?
Вы можете использовать 'описание', чтобы сгладить его в строку. –