OK, so I know this question seems really basic, but I can't find a solution to my problem anywhere -
I have a .plist file within my project entitled 'feelings'. My plist file reads:
<plist version="1.0">
<dict>
<key>feelings</key>
<array>
<string>Happy<开发者_StackOverflow社区;/string>
<string>Sad</string>
</array>
</dict>
</plist>
In the .m file under -(void)ViewDidLoad; I am trying to retrieve the contents of the plist using the following code:
NSString *path = [[NSBundle mainBundle] pathForResource:@"feelings" ofType:@"plist"];
NSArray *tempArray = [[NSArray alloc] initWithContentsOfFile:path];
self.feelingsArray = tempArray;
[tempArray release];
However I don't seem to be loading the array from the .plist file and executing the following code:
NSLog(@"feelings: %@", feelings);
returns (null)
Can anyone figure out where I'm going wrong? It all seems fine to me, from what I've worked on before.
It returns a dictionary object not array,
NSString *path = [[NSBundle mainBundle] pathForResource:@"feelings" ofType:@"plist"];
NSDictionary *tempDict = [[NSDictionary alloc] initWithContentsOfFile:path];
NSLog(@"Feelings: %@",tempDict);
[tempDict release];
Your top-level of the plist file is a dictionary, not an array. You should use NSDictionary
instead of NSArray
:
NSDictionary *tempDict = [[NSDictionary alloc] initWithContentsOfFile:path];
self.feelingsArray = [tempDict objectForKey:@"feelings"];
[tempDict release];
精彩评论