0

辞書のリスト(item0、item1、item2)を含むリストを含むPlistがあり、このplistをグラフに入力します..それは正常に動作し、Plistで.次key (date)-> Value(i store by using NSDate)のようにPlistをソートする必要があります graph。 1週間だけ展示。最初の値が最大 (1 週間) の値26-Dec-12よりも多い場合、 1-Jan-13plist に表示する必要があります。

最初 .

コード:

- (NSArray *)readFromPlist
{
// get paths from root direcory
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                             NSUserDomainMask, YES); 
NSString *documentsDirectory = [documentPaths objectAtIndex:0];
NSString *documentPlistPath = [documentsDirectory stringByAppendingPathComponent:@"calori.plist"];

NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:documentPlistPath];

valueArray = [dict objectForKey:@"title"];

  return valueArray;
}

- (void)drawRect:(CGRect)rect {
// Drawing code


    CGContextRef _context = UIGraphicsGetCurrentContext();
ECGraph *graph = [[ECGraph alloc] initWithFrame:CGRectMake(10,10, 480, 320) 
                                    withContext:_context isPortrait:NO];

NSMutableArray *Array=[NSMutableArray arrayWithArray:[self readFromPlist]];


NSMutableArray *items = [[NSMutableArray alloc] init];

for (id object in [Array reverseObjectEnumerator]){

    if ([object isKindOfClass:[NSDictionary class]])
    {
        NSDictionary *objDict = (NSDictionary *)object;


           tempItemi  =[[ECGraphItem alloc]init];

        NSString *str=[objDict objectForKey:@"title"];
        NSLog(@"str value%@",str);
        float f=[str floatValue];


        NSString*str1=[objDict objectForKey:@"date"];

        NSLog(@" str values2-- %@",str1);

        tempItemi.isPercentage=YES;
        tempItemi.yValue=f;

        tempItemi.name=str1;



         [items addObject: tempItemi];



    }
}

[graph drawHistogramWithItems:items lineWidth:2 color:[UIColor blackColor]];
}
4

3 に答える 3

1

NSSortDescriptor を試しましたか?

NSSortDescriptor *sortDesc = [[NSSortDescriptor alloc] initWithKey:@"DATE" ascending:YES selector:@selector(compare:)];
[yourDictionary sortUsingDescriptors:[NSArray arrayWithObject:sortDesc]];
于 2012-12-26T11:28:53.650 に答える
1

これを試して

NSString *path = [[NSBundle mainBundle] pathForResource:@"yourfile" ofType:@"plist"];
    NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path];
    // sort it
    NSArray *sortedArray = [[myDict allKeys] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
    // iterate and print results
    for(NSString *key in sortedArray) {
        NSLog(@"key=%@,value=%@", key, [dict objectForKey:key]);
    }
于 2012-12-26T11:34:24.500 に答える
1

要件は7日以内の日付をフィルタリングすることであるため、

私はあなたに論理を与えています、この方法を試してください:

- (NSArray *)readFromPlistForOneWeek {

    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES); 
    NSString *documentsDirectory = [documentPaths objectAtIndex:0];
    NSString *documentPlistPath = [documentsDirectory stringByAppendingPathComponent:@"calori.plist"];


    NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:documentPlistPath];

    //loop through each of the item
    //and check if <8 then add that keyValue to array
    NSMutableArray *tempValueArray=[NSMutableArray new];
    for (NSDictionary *subDict in [dict objectForKey:@"title"]) {
       // NSLog(@"=> %@",subDict);

        NSString *plistDateString=[subDict objectForKey:@"date"];
        NSDate *currentDate = [NSDate date];

        NSDateFormatter *dateFormatter=[NSDateFormatter new];
        [dateFormatter setDateFormat:@"dd-MMM-yy"];

        NSDate *plistDate=[dateFormatter dateFromString:plistDateString];

        NSString *currentDateString=[dateFormatter stringFromDate:currentDate];

        NSTimeInterval secondsBetween = [plistDate timeIntervalSinceDate:currentDate];
        NSInteger dateDiff = secondsBetween / 86400;

        if( dateDiff<8 ){ //within 0-7 days
            [tempValueArray addObject:subDict];
        }
    }

    NSLog(@"valuArray : %@",tempValueArray);

    return tempValueArray;
}
于 2012-12-26T11:39:55.417 に答える