从NSDictionary检索键/值对会导致数据不正确并导致崩溃

时间:2012-01-18 06:56:58

标签: ios ios4 ios5

我的字典包含两个键值对date / text ..其中日期也是字符串格式,现在我正在提取键和值并将其传递给其他函数..以下面的方式

 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);
        } 

我的词典包含两个日期,即1月1日和3月1日..但每当我遍历循环时,只有第一个jan通过,而第3个jan没有通过,所以,朋友们,请帮助我理解哪里我错了......有时候它会崩溃..

此致 兰吉特

1 个答案:

答案 0 :(得分:1)

我认为不是

keys = [Dictionary allKeys];

您打算使用:

keys = [m_dateNoteDict allKeys];