[英]“Terminating app due to uncaught exception 'NSInvalidArgumentException'” error with stringByTrimmingCharactersInSet on iPhone
I am reading JSON data back from a server, and I am trying to parse a particular string that contains an HTML URL. 我正在从服务器读回JSON数据,并且试图解析包含HTML URL的特定字符串。 The data is in an NSString called current_weatherIconUrl2.
数据位于名为current_weatherIconUrl2的NSString中。 The console output from Xcode is below:
Xcode的控制台输出如下:
2010-07-06 17:17:46.628 WhatToDo[13437:20b] Current Condition: URL2:(" http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0001_sunny.png ")
2010-07-06 17:17:46.628 WhatToDo [13437:20b]当前条件:URL2 :(“ http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0001_sunny.png ”)
What I tried to do is use "stringByTrimmingCharactersInSet" to parse the '(', '"', and also the line feed and newlines, but I am instead getting the NSInvalidArgumentException: 我想做的是使用“ stringByTrimmingCharactersInSet”来解析'(','“',以及换行符和换行符,但是我却得到了NSInvalidArgumentException:
2010-06-30 00:18:07.357 WhatToDo[7299:20b] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[NSCFArray stringByTrimmingCharactersInSet:]: unrecognized selector sent to instance 0x3e84970'
2010-06-30 00:18:07.357 WhatToDo [7299:20b] *由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'* -[NSCFArray stringByTrimmingCharactersInSet:]:无法识别的选择器已发送至实例0x3e84970'
Here is an extract of the data that is returned from the server (information I want to extract is bolded): 这是从服务器返回的数据的摘录(我要提取的信息以粗体显示):
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;
}
);
};
}
Here is the code that parses the data into NSString variables. 这是将数据解析为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]];
}
Can someone tell me what I am doing wrong or if there is a better method than using stringByTrimmingCharactersInSet
? 有人可以告诉我我做错了什么,还是有比使用
stringByTrimmingCharactersInSet
更好的方法?
The error you're getting actually describes what goes wrong: 您实际上得到的错误描述了出了什么问题:
2010-06-30 00:18:07.357 WhatToDo[7299:20b] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[NSCFArray stringByTrimmingCharactersInSet:]: unrecognized selector sent to instance 0x3e84970'
2010-06-30 00:18:07.357 WhatToDo [7299:20b] *由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因: '*-[NSCFArray stringByTrimmingCharactersInSet:]:无法识别的选择器已发送至实例0x3e84970'
You're trying to send an unrecognized selector ( stringByTrimmingCharactersInSet:
) to an instance of NSCFArray
( NSArray
). 您正在尝试将无法识别的选择器(
stringByTrimmingCharactersInSet:
发送到NSCFArray
( NSArray
)的实例。
Obviously, this selector only exists on NSString
, so this won't work. 显然,此选择器仅存在于
NSString
,因此将不起作用。
It appears that [current_weatherIconUrl valueForKey:@"value"]
really is an NSArray
, not an NSString
. 看来
[current_weatherIconUrl valueForKey:@"value"]
实际上是一个NSArray
,而不是NSString
。
Thanks for your help Douwe M. I was able to make some modifications, and now the code creates the variables I need. 感谢您的帮助DouweM。我能够进行一些修改,现在代码创建了我需要的变量。 See below...
见下文...
NSArray *weatherIconUrl2= [weatherIconUrl valueForKey:@"value"];
NSString *weatherIconUrl3 = [[weatherIconUrl2 valueForKey:@"description"] componentsJoinedByString:@""];
[imageArray addObject:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:weatherIconUrl3]]]];
-Todd -托德
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.