iphone - 如何按值对 plist 进行排序

标签 iphone ios plist sorting

我有带有字典列表(item0,item1,item2)的列表的 Plist。我在图表中填充了这个 plist。它工作正常。在 Plist key(date)->Value(i store通过使用 NSDate) 。现在我需要按照以下方式对 Plist 进行排序:- graph 应该只显示一周。 假设第一个值是 26-Dec-12 而不是只有 1-Jan-13(1 周)值 plist 应该显示

first .

代码:

- (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]];
}

最佳答案

由于您的要求是过滤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;
}

关于iphone - 如何按值对 plist 进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14039913/

相关文章:

iphone - 在 iOS 上通过网站收集物理签名的方法

iphone - iphone模拟器是否能够显示航向和纬度,经度?

ios - 下载 plist,但似乎每次都得到一个缓存版本

iphone - 在 iPhone 源和项目资源上存储 key

iphone - 在应用程序购买/ View Controller 崩溃 : Message sent to deallocated instance

ios - SpriteKit : Passing variable from GameViewController to GameScene

ios - 应用程序退出后 Swift Struct bool 值会重置

ios - 自定义 SearchBar iOS

iphone - 尝试创建 pList 但它没有被创建

iphone - 另一个 UISplitViewController + UITabBarController 问题