2012-12-26 147 views
0

我有名單與字典清單(item0,item1,item2).. 一般當我在屏幕上顯示..它填充在升序... item0 then item1 then item2......and so on ..我想Plist按相反順序顯示爲itemn,itemn-1.........item2,item1,item0Plist以相反的順序

first
這是代碼是如何去...我怎麼能扭轉它在Array ..need下面

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 
} 

回答

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--) 
+0

烏拉圭回合努力感謝雅man..thanks!它的作品! – Christien

+0

,這是可能的...像plist ..我想顯示爲1周迭代...意味着日期2012年12月26日至1周 – Christien

+0

plists按鍵值對存儲,您可以輕鬆地排序字典或基於鍵或值回收它... –

1

您正在從count to 1 itrating而陣列具有從索引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 
} 
+0

雅感謝男人!有用!! thnx爲你的努力 – Christien

+0

,這是可能的...像plist ..我想顯示爲1周的迭代...意味着日期-2012年12月26日至1周 – Christien