2012-06-11 48 views
1

我有一个plist文件,里面有6个项目。NSDictionary + NSArray + plist

enter image description here

当我将其加载到一个数组,并显示在一个表视图,它未排序像上面和看起来像这样:

enter image description here

这是我的代码中号使用:

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

它仍然未排序磨片我用allValues

我该怎么做才对?

回答

0

这里的解决方案:

- (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); 
    } 
} 
1

据我了解,NSDictionary的(讽刺)不下令从plist中的任何具体方式进行的项目(字母,数字的,值,指数等),你必须使用NSArray才能保留plist的命令。

我一直没能解决我的,但这种联系似乎告诉别人有...

Plist Array to NSDictionary