简体   繁体   English

从plist数组创建NSArray

[英]Create NSArray from plist array

I need to pull out data from a plist array and put it in an NSArray. 我需要从plist数组中提取数据并将其放入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. 我的plist文件。 Named: htmlData.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>

It should set self.sitesArray equal to @"<html>", @"<body>, nil; but it is not working. 它应该将self.sitesArray设置self.sitesArray等于@"<html>", @"<body>, nil;但是它不起作用。

First, understand that dictionary and tempDictionary are pointing to the same object, and you cannot access the object after you release it. 首先,要了解dictionarytempDictionary指向同一个对象,并且释放该对象后将无法访问它。 So when you do [tempDictionary release] , the dictionary is released. 因此,当您执行[tempDictionary release] ,将[tempDictionary release]字典。 It cannot be then accessed through the dictionary pointe. 然后就不能通过dictionary指针访问它。

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. 您对sitesArray设置器应保留该数组,否则对[dictionary release]的调用将释放该数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM