Plist按相反顺序排列

时间:2012-12-26 10:15:59

标签: iphone ios nsarray plist

我的Plist列表包含词典列表(item0,item1,item2).. 通常当我在屏幕上显示..按升序填充... item0 then item1 then item2......and so on ..我希望Plist以相反的顺序显示为itemn,itemn-1.........item2,item1,item0

first
这就是代码的用法......以及如何将其反转.. Array下面的

}需要进行更改
NSMutableArray *Array=[NSMutableArray arrayWithArray:[self readFromPlist]];

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

for (int i = 0; i< [Array count]; i++)
//how to reverse by making changes here
//for (int i =[Array count] ; i>0; i--) does not work
{


    id object = [Array objectAtIndex:i];

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


        //my code here
 }

2 个答案:

答案 0 :(得分:1)

使用反向枚举

for (id someObject in [myArray reverseObjectEnumerator]){
    // print some info
    NSLog([someObject description]);
}

代码:

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

        //my code here
    }
}

同样在您的代码中:

//for (int i =[Array count] ; i>0; i--) does not work

应该是

for (int i =[Array count]-1 ; i>=0; i--)

答案 1 :(得分:1)

您正在从count to 1开始,而数组包含来自索引count-1 to 0的对象 例如
包含10个对象的数组具有从索引09

的对象
for (int i =[Array count]-1 ; i >= 0; i--)
{
    id object = [Array objectAtIndex:i];

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


        //my code here
 }