[英]Terminating app due to uncaught exception 'NSInvalidArgumentException', NSUserDefaults?
[英]“Terminating app due to uncaught exception 'NSInvalidArgumentException'” error with stringByTrimmingCharactersInSet on iPhone
我正在從服務器讀回JSON數據,並且試圖解析包含HTML URL的特定字符串。 數據位於名為current_weatherIconUrl2的NSString中。 Xcode的控制台輸出如下:
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] *由於未捕獲的異常'NSInvalidArgumentException'而終止應用程序,原因:'* -[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
更好的方法?
您實際上得到的錯誤描述了出了什么問題:
2010-06-30 00:18:07.357 WhatToDo [7299:20b] *由於未捕獲的異常'NSInvalidArgumentException'而終止應用程序,原因: '*-[NSCFArray stringByTrimmingCharactersInSet:]:無法識別的選擇器已發送至實例0x3e84970'
您正在嘗試將無法識別的選擇器( stringByTrimmingCharactersInSet:
發送到NSCFArray
( NSArray
)的實例。
顯然,此選擇器僅存在於NSString
,因此將不起作用。
看來[current_weatherIconUrl valueForKey:@"value"]
實際上是一個NSArray
,而不是NSString
。
感謝您的幫助DouweM。我能夠進行一些修改,現在代碼創建了我需要的變量。 見下文...
NSArray *weatherIconUrl2= [weatherIconUrl valueForKey:@"value"];
NSString *weatherIconUrl3 = [[weatherIconUrl2 valueForKey:@"description"] componentsJoinedByString:@""];
[imageArray addObject:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:weatherIconUrl3]]]];
-托德
您的json響應格式可能不正確。 因此,請在jsonlint上驗證您的JSON響應
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.