从plist数组创建NSArray

Jak*_*r00 0 iphone objective-c

我需要从plist数组中提取数据并将其放入NSArray中.但它似乎不起作用.

这是我的主要文件:

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];
Run Code Online (Sandbox Code Playgroud)

我的plist文件.命名:htmlData.plist

<plist version="1.0">
<dict>
    <key>tagName</key>
        <array>
            <string>&lt;html&gt;</string>
            <string>&lt;body&gt;</string>
        </array>
</dict>
</plist>
Run Code Online (Sandbox Code Playgroud)

它应该设置为self.sitesArray等于@"<html>", @"<body>, nil;但它不起作用.

Nic*_*ore 5

首先,要明白dictionarytempDictionary都指向同一个对象,你放开后,您不能访问该对象.所以当你这样做时[tempDictionary release],字典就会被释放.然后无法通过dictionarypointe 访问它.

对于此代码,请尝试:

NSString *path = [[NSBundle mainBundle] pathForResource:@"htmlData" ofType:@"plist"];
NSDictionary *dictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:path];  
self.sitesArray = [dictionary objectForKey:@"tagName"];
[dictionary release];
Run Code Online (Sandbox Code Playgroud)

你的setter sitesArray应该保留数组,或者调用[dictionary release]将释放它.