Ranjit
Ranjit

Reputation: 4646

retrieving key/value pairs from NSDictionary leads to incorrect data and also crash

my dictionary contains two key value pairs date/text..here the date is also in string format,now I am extracting the keys and values and passing it to other function..in the below manner

 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.timeStyle = NSDateFormatterNoStyle;

    dateFormatter.dateFormat = @"dd-MMM-yyyy";     

        keys   = [Dictionary allKeys];
        values = [m_dateNoteDict allValues];

        for(int i = 0 ; i<[keys count]; i++)
        {            
            NSString *notes   = [values objectAtIndex:i];
            NSDate *noteDate  = [dateFormatter dateFromString:[keys objectAtIndex:i]];


            NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
            NSDateComponents *weekdayComponents = [gregorian components:(NSDayCalendarUnit | NSWeekdayCalendarUnit | NSYearCalendarUnit | NSMonthCalendarUnit)fromDate:[self getCurrentDate]];

            NSInteger year  = [weekdayComponents year];


            [gregorian setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
            NSDateComponents *timeZoneComps=[[NSDateComponents alloc] init];

            [timeZoneComps setYear:year];
            [timeZoneComps setHour:00];
            [timeZoneComps setMinute:00];
            [timeZoneComps setSecond:01];

            noteDate =[gregorian dateFromComponents:timeZoneComps];         

            [self saveNotes:0 :noteDate :notes];//passing the date to this function
            NSLog(@"dates:%@",noteDate);
        } 

my dictionary contains two dates i.e 1 jan and 3rd jan..but each time when I iterate through the loop ,only first jan is passed,and 3rd jan is not passed,So,friends,please help me out in understanding where I am going wrong..and sometimes its crash..

Regards Ranjit

Upvotes: 0

Views: 467

Answers (1)

jonkroll
jonkroll

Reputation: 15722

I think instead of

keys = [Dictionary allKeys];

you meant to use:

keys = [m_dateNoteDict allKeys];

Upvotes: 1

Related Questions