开发者

Creating Arrays from a plist file in iphone SDK 3.3 and up : objective c

开发者 https://www.devze.com 2023-01-05 05:11 出处:网络
I have run into this issue and have put some major time into finding the answer. I am somewhat new to objective c but not to programming.

I have run into this issue and have put some major time into finding the answer. I am somewhat new to objective c but not to programming.

Here is my question.

I have a plist file with this structure

root {
  A (
  开发者_运维百科     {songTitle : contents of song},
       {songTitle : contents of song}
  ),
  B (
       {songTitle : contents of song}
  ),
  C (
       {songTitle : contents of song}
  ),
... kepps going
}

Sorry if the the plist structure is not correct.

Pretty much I have a root dictionary (that is what it comes with) that contains an array of A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,...Z (alphabet)

Each letter of the alphabet array contains 1 or more dictionaries that have a key, value pair of songTitle (this could be any string) as the key and the song lyrics for the value.

My issue here is I need to create an array of all song titles and have been having a rough time trying to find out how to do this. I own 4 books on object c and none of them go into detail about multidimensional arrays and how to access pieces inside them.

I have created an array with all the letters and have created an array that contains the objects from each letter.

Like I stated before I need to find out how to make an array that contains each song title.

If you can help me that would save me a lot of time.

Thanks, Wes


I am guessing you are suggesting I change my root from a dictionary to an array?

Maybe it might be better to show my code here. Also I have attached an updated version of my plist file

Sorry seems I cannot add the image here but you can view it http://www.wesduff.com/images/forum_images/plist_examp.png

So as you can see I have updated the plist file to show the array of letters that each contain multiple dictionaries. Each dictionary has a songTitle and a songLyrics.

How can I write code to get an array of songTitles.

Here is what I have come up with so far

NSString *path = [[NSBundle mainBundle] pathForResource:@"songs" ofType:@"plist"];
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path];
//This gives me an array of all the letters in alphabetical order
NSArray *array = [[dict allKeys] sortedArrayUsingSelector:@selector(compare:)];

/** 
Now I need to find out how to get an array of all songTitles
**/

I am still working on this and looking through what others have written but have not found anything yet.

As the first answer has suggested, should I change the root to an array or keep it as I have it in this plist image I have attached.

Thanks again,

Wes


Ok so I did some more digging and came up with this from the plist file that was included in this picture

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;

}

I am sure there is probably a better way to do this but this is what I have come up with on my own. Thanks


If you have single array with the contents of all the letters, the rest is fairly simple. Iterate through the objects and call the dictionary method allKeys on each one. Each call to allKeys will return an NSArray containing the keys of that specific dictionary, which you can then place into another array.

EDIT

I made a mistake, didn't go deep enough. This is what I would do:

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;

This array isn't guaranteed to be sorted alphabetically, so you'll have to call [sortedArrayUsingSelector:] on it.

Reference:
NSMutableArray Class Reference
NSDictionary Class Reference

0

精彩评论

暂无评论...
验证码 换一张
取 消