NSXML解析器返回重复的值并插入到两次coredata

我被NSXML解析器解析谷歌的天气API。 我试图让我的日期与天气要素成coredata一起。 在此之前,我将它们存储在一个NSDictionary的。 但我正在逐渐dupliacte值。 我张贴我的代码一些部分:

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict { if ([@"forecast_date" isEqualToString:elementName]) { NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateFormat:@"yyyy-MM-dd"]; startDate = [formatter dateFromString:[attributeDict objectForKey:@"data"]]; [formatter release]; } else if ([@"forecast_conditions" isEqualToString:elementName]) { isParsingForecast = YES; isParsingInformation = NO; isParsingCurrent = NO; newdate=[startDate addTimeInterval:60*60*24*[forecastConditions count]]; NSMutableDictionary *fields = [[NSMutableDictionary alloc] init]; [fields setObject:newdate forKey:@"date"]; [fields setObject:city forKey:@"city"]; [fields setObject:state forKey:@"state"]; [fields setObject:country forKey:@"country"]; [forecastConditions addObject:fields]; [fields release]; } else if (isParsingForecast) { NSMutableDictionary *fields = [forecastConditions lastObject]; NSLog(@"dic is : %@ \n\n",fields); [fields setObject:[attributeDict objectForKey:@"data"] forKey:elementName]; } }

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName { if ([@"forecast_conditions" isEqualToString:elementName]) { NSMutableDictionary *fields=[forecastConditions lastObject]; NSString*icon=[fields objectForKey:@"icon"]; NSLog(@" the MAIN ICON IS %@",icon); NSString *all=[icon lastPathComponent]; [forecastConditions addObject:fields]; [fields release]; // NSLog(@"the icon is %@",icon); [fields setObject:all forKey:@"icon"]; isParsingForecast = NO; } else if([@"forecast_information" isEqualToString:elementName]) { NSMutableDictionary *fields=[forecastConditions lastObject]; NSString*forecast_date=[fields objectForKey:@"forecast_date"]; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"yyyy-MM-dd "]; NSDate *dateFromString; dateFromString = [dateFormatter dateFromString:forecast_date]; NSLog(@" the forecast info IS %@",dateFromString); isParsingInformation=NO; } }

分类:iphone 时间:2015-03-15 人气:0
本文关键词: iPhone,目标C
分享到:

相关文章

Copyright (C) 55228885.com, All Rights Reserved.

55228885 版权所有 京ICP备15002868号

processed in 0.323 (s). 10 q(s)