【问题标题】:Creating Arrays from a plist file in iphone SDK 3.3 and up : objective c从 iphone SDK 3.3 及更高版本中的 plist 文件创建数组:目标 c
【发布时间】:2010-06-25 14:33:15
【问题描述】:

我遇到了这个问题,并花了一些时间来寻找答案。我对目标 c 有点陌生,但对编程不熟悉。

这是我的问题。

我有一个具有这种结构的 plist 文件

根 { 一种 ( {songTitle : 歌曲内容}, {songTitle : 歌曲内容} ), 乙( {songTitle : 歌曲内容} ), C ( {songTitle : 歌曲内容} ), ...继续前进 }

如果 plist 结构不正确,请见谅。

几乎我有一个根字典(这是它附带的),其中包含 A、B、C、D、E、F、G、H、I、J、K、L、M、N 的数组,O,P,...Z(字母)

字母数组的每个字母都包含 1 个或多个字典,这些字典有一个键、值对 songTitle(这可以是任何字符串)作为键,歌词作为值。

我的问题是我需要创建一个包含所有歌曲标题的数组,并且一直在努力寻找如何做到这一点。我拥有 4 本关于对象 c 的书,其中没有一本详细介绍多维数组以及如何访问其中的片段。

我创建了一个包含所有字母的数组,并创建了一个包含每个字母的对象的数组。

就像我之前所说的,我需要找出如何创建一个包含每首歌曲标题的数组。

如果你能帮助我,那将节省我很多时间。

谢谢, 韦斯

【问题讨论】:

    标签: iphone objective-c multidimensional-array plist


    【解决方案1】:

    我猜您是在建议我将根目录从字典更改为数组?

    也许在这里显示我的代码可能会更好。 我还附上了我的 plist 文件的更新版本

    抱歉,我无法在此处添加图片,但您可以查看 http://www.wesduff.com/images/forum_images/plist_examp.png

    如您所见,我已更新 plist 文件以显示包含多个字典的字母数组。每个字典都有一个 songTitle 和一个 songLyrics。

    如何编写代码来获取歌曲标题数组。

    这是我目前的想法

    NSString *path = [[NSBundle mainBundle] pathForResource:@"songs" ofType:@"plist"]; NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path]; //这给了我一个按字母顺序排列的所有字母的数组 NSArray *array = [[dict allKeys] sortedArrayUsingSelector:@selector(compare:)]; /** 现在我需要找出如何获取所有歌曲标题的数组 **/

    我仍在努力,并查看其他人写的内容,但还没有找到任何东西。

    正如第一个答案所建议的那样,我应该将根更改为数组还是保留它在我附加的这个 plist 图像中的样子。

    再次感谢,

    韦斯

    【讨论】:

      【解决方案2】:

      好的,所以我做了更多的挖掘,并从这张图片中包含的 plist 文件中找到了这个

      http://www.wesduff.com/images/forum_images/plist_examp.png

      - (void)viewDidLoad {
      //path for plist file
      NSString *path = [[NSBundle mainBundle] pathForResource:@"songList" ofType:@"plist"];
      
      //dictionary created from plist file
      NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path];
      
      //release the path because it is no longer needed
      [path release];
      
      //temp array to hold an array of all alphabetical letters
      NSArray *array = [[dict allKeys] sortedArrayUsingSelector:@selector(compare:)];
      //assign array to allLetters array
      self.allLetters = array;
      
      //Create two mutable arrays for the songArray (could do a little cleaner job of this here)
      NSMutableArray *songArray = [[NSMutableArray alloc] init];
      NSMutableArray *songTitles = [[NSMutableArray alloc] init];
      
      //Add messy array to songArray then we can work with the songArray (maybe something better to do here)
      for(id key in dict)
      {
          [songArray addObject:[dict objectForKey:key]];
      }
      
      //temp array to hold a messy array for all of the songTitles
      NSArray *tempArray = [songArray valueForKey:@"songTitle"];
      
      //go through the temparray and clean it up to make one array of all song titles and sort them
      for (NSUInteger i = 0; i < [tempArray count]; i++) {
          [songTitles addObjectsFromArray:[[tempArray objectAtIndex:i] sortedArrayUsingSelector:@selector(compare:)]];
      }
      
      //assign all song titles to our array of songTitles
      self.allSongTitles = songTitles;
      
      [dict release];
      [allSongTitles release];
      [songArray release];
      [tempArray release];
      [array release];
      [super viewDidLoad];
      
      // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
      // self.navigationItem.rightBarButtonItem = self.editButtonItem;
      

      }

      我确信可能有更好的方法来做到这一点,但这是我自己想出的。谢谢

      【讨论】:

      • 我很快就会有一个经过编辑的答案,但我想先指出一些事情。看到第一个循环,for(id key in dict)?您称为key 的变量命名错误,它不是遍历字典的键,而是遍历值。如果您将循环更改为读取for(id key in [dict allKeys]),那么它将遍历键集。附带说明,当您向问题添加信息时,请编辑原始问题。这不是一个论坛,问题下方的任何内容都应该是一个答案尝试。如果您需要,请在其他人的回答下留下评论。
      • @Endemic:你错了。请参阅 developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/… 部分采用快速枚举:“NSDictionary 枚举其键”
      【解决方案3】:

      如果你有一个包含所有字母内容的数组,剩下的就相当简单了。遍历对象并在每个对象上调用字典方法allKeys。每次调用 allKeys 都会返回一个 NSArray,其中包含该特定字典的键,然后您可以将其放入另一个数组中。

      编辑

      我犯了一个错误,没有深入。这就是我会做的:

      NSString *path = [[NSBundle mainBundle] pathForResource:@"songs" ofType:@"plist"];
      NSDictionary plistDict = [NSDictionary dictionaryWithContentsOfFile:path]; //not using alloc and init means this isn't retained, so it will be autoreleased at the end of the method
      NSArray *allLetterContents = [plistDict allValues]; // array of arrays, where each element is the content of a 'letter' in your plist (i.e. each element is an array of dictionaries)
      
      NSMutableArray *allSongTitles = [[[NSMutableArray alloc] init] autorelease];
      
      for(NSArray *oneLetterContents in allLetterContents)
      {
          for(NSDictionary *song in oneLetterContents)
          {
              [allSongTitles addObject:[song objectForKey:@"songTitle"]]
          }
      }
      
      return allSongTitles;
      

      此数组不保证按字母顺序排序,因此您必须对其调用 [sortedArrayUsingSelector:]。

      参考:
      NSMutableArray Class Reference
      NSDictionary Class Reference

      【讨论】:

        猜你喜欢
        • 2022-11-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-07-17
        • 1970-01-01
        • 1970-01-01
        • 2017-09-03
        相关资源
        最近更新 更多