0

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

第一的 .
这就是代码的运行方式...以及如何反转它..需要在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
 }
4

2 回答 2

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--)
于 2012-12-26T10:20:10.473 回答
1

您正在迭代,count to 1而数组有来自索引的对象,count-1 to 0
例如
带有10对象的数组有来自索引0的对象9

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
 }
于 2012-12-26T10:22:00.237 回答