2011-07-15 28 views
1

我想将我的nsdate添加到nsdictionary.CAn任何人都告诉我如何添加它?无法将NSDate添加到NSDictionary

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict 
    {  
     //if(conditionCount ==0){ 

     if([@"forecast_information" isEqualToString:elementName]) { 
      //conditionDate = get date 
      NSDate *now=[NSDate date]; 

      isParsingInformation=YES; 
      NSArray *array=[NSArray arrayWithObject:now]; 
      NSLog(@" the time is %@",array); 

      [forecastConditions addObject:[NSMutableDictionary dictionary]]; 

     } 
     else if([@"forecast_date" isEqualToString:elementName]) 
     { 
      if(!forecast_information) 
       forecast_information=[[NSMutableArray alloc]init]; 
     } 
     else if(isParsingInformation){ 
      NSMutableDictionary *field=[forecastConditions lastObject]; 
      [field setObject:[attributeDict objectForKey:@"data"] forKey:elementName];    
     } 

我天玑know..see我真正想要做的是我得到我的谷歌API的天气在名为fields..I一个NSDictionary想从系统的NSDictionary的第一指数加我的NSDate。 .I NSdictionary我有两个数据,我想在第一个索引处添加我的nSdate ..我无法做到这一点。 我想通过每个循环的日期增加...如何做到这一点?

+0

而究竟是什么不成? –

回答

0

我认为这是不是最新的数据

[field setObject:[attributeDict objectForKey:@"date"] forKey:elementName]; 

更新的代码

NSMutableDictionary *dic=[[NSMutableDictionary alloc] init];//creation 


[dic setObject:[NSDate date] forKey:@"Today"];//added 

NSLog(@"dic is : %@ \n\n",dic); 
+0

我dnt知道..看到我真正想要做的是我得到我的谷歌天气api在nsdictionary命名fields ..我想添加我的NSDate从系统在nsdictionary的第一个索引..我NSdictionary我夫妇数据,我想添加我的nSdate在第一个索引..我无法做到这一点。 – novice

+0

立即检查这个 –

+0

谢谢了很多....我也想在每个循环后的一天增加日期.... – novice

0
NSDate *now = [NSDate date]; 

    int daysToAdd = 50; // or 60 :-) 

    NSDate *newDate1 = [now addTimeInterval:60*60*24*daysToAdd]; 

    NSLog(@"Quick: %@", newDate1); 

OR

NSDate *now = [NSDate date]; 

    int daysToAdd = 50; // or 60 :-) 


     // set up date components 

    NSDateComponents *components = [[[NSDateComponents alloc] init] autorelease]; 

    [components setDay:daysToAdd]; 


     // create a calendar 

    NSCalendar *gregorian = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease]; 



    NSDate *newDate2 = [gregorian dateByAddingComponents:components toDate:now options:0]; 

    NSLog(@"Clean: %@", newDate2); 
+0

好..但如何通过解析器增加我的nsdate? – novice