开发者

Create NSArray from plist array

开发者 https://www.devze.com 2023-01-22 10:31 出处:网络
I need to pull out data from a plist array and put it in an NSArray. But it seems to not be working. Here is my main file:

I need to pull out data from a plist array and put it in an NSArray. But it seems to not be working.

Here is my main file:

NSString *path = [[NSBundle mainBundle] pathForResource:@"htmlData" ofType:@"plist"];
NSMutableDictionary *tempDictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:path];

dictionary = tempDictionary;
[tempDictionary release];

NSMutableArray *nameArray = [[NSMutableArray alloc] init];
nameArray = [dictionary objectForKey:@"tagName"];

self.sitesArray = nameArray;

[nameArray release];

My plist file. Named: htmlData.plist

<plist version="1.0">
<dict>
    <key>tagN开发者_StackOverflow中文版ame</key>
        <array>
            <string>&lt;html&gt;</string>
            <string>&lt;body&gt;</string>
        </array>
</dict>
</plist>

It should set self.sitesArray equal to @"<html>", @"<body>, nil; but it is not working.


First, understand that dictionary and tempDictionary are pointing to the same object, and you cannot access the object after you release it. So when you do [tempDictionary release], the dictionary is released. It cannot be then accessed through the dictionary pointe.

For this code, try:

NSString *path = [[NSBundle mainBundle] pathForResource:@"htmlData" ofType:@"plist"];
NSDictionary *dictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:path];  
self.sitesArray = [dictionary objectForKey:@"tagName"];
[dictionary release];

Your setter for sitesArray should be retaining the array, or the call to [dictionary release] will release it.

0

精彩评论

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

关注公众号