1

I have a plist file which has 6 items in it.

enter image description here

when I load it into an array and show it in a table view, it is not sorted like above and looks like this:

enter image description here

this is the code I'm using:

NSString* plistPath = [[NSBundle mainBundle] pathForResource:@"ghazal2" ofType:@"plist"];
myDictionary = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
myArray = [myDictionary allKeys];

it's still unsorted whe I use allValues

how can I make it right?

Milad
  • 1,239
  • 3
  • 19
  • 37

2 Answers2

1

From what I understand, NSDictionary (ironically) doesn't order the items from a plist in any particular way (alphabetical, numberical, value, index, etc), you'd have to use NSArray in order to preserve the order from the plist.

I haven't been able to fix mine, but this link seems to show others have...

Plist Array to NSDictionary

Community
  • 1
  • 1
0

here's the solution:

- (void)viewDidLoad
{
    [super viewDidLoad];

    if (!myArray2) // myArray2 is NSMutableArray
    {
        myArray2 = [[NSMutableArray alloc] init];
    }

    NSString *path = [[NSBundle mainBundle] pathForResource:@"list" ofType:@"plist"];
    NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path];

    NSArray *sortedArray = [[dict allKeys] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];

    for(NSString *key in sortedArray) 
    {      
        [myArray2 addObject:[dict objectForKey:key]];
    }

    for(NSString *key in myArray2) 
    {      
        NSLog(@"it is: %@", key);
    }   
}
Milad
  • 1,239
  • 3
  • 19
  • 37