【问题标题】:NSDictionary + NSArray + plistNSDictionary + NSArray + plist
【发布时间】:2012-06-11 13:50:40
【问题描述】:

我有一个 plist 文件,其中包含 6 个项目。

当我将它加载到数组中并在表格视图中显示时,它没有像上面那样排序,看起来像这样:

这是我正在使用的代码:

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

我使用allValues时仍然未排序

我怎样才能使它正确?

【问题讨论】:

    标签: nsarray plist nsdictionary


    【解决方案1】:

    据我了解,NSDictionary(具有讽刺意味)不会以任何特定方式(字母、数字、值、索引等)对 plist 中的项目进行排序,您必须使用 NSArray 才能保留顺序来自 plist。

    我无法修复我的问题,但这个链接似乎表明其他人已经...

    Plist Array to NSDictionary

    【讨论】:

      【解决方案2】:

      解决办法如下:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-10
        • 2011-01-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多