2011-07-15 27 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