0

我们如何检索存储在 plist 文件中的数组数据。

plist文件和源代码如下图,

------------------------plist File-------------------------
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Name</key>
    <string>abcxxx</string>
    <key>Phones</key>
    <array/>
    <key>item1</key>
    <string>121327777</string>
    <key>item2</key>
    <string>333333333</string>
</dict>
</plist>
---------------------------------------------------------



  -(id) init
{


    NSString *errorDesc = nil;
    NSPropertyListFormat format;
    phoneNumbers =[[NSMutableArray alloc]init];
    NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"testfile" ofType:@"plist"];
    NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:plistPath];

    NSDictionary *temp = (NSDictionary *)[NSPropertyListSerialization
                                          propertyListFromData:plistXML
                                          mutabilityOption:NSPropertyListMutableContainersAndLeaves
                                          format:&format errorDescription:&errorDesc];

    if(!temp){
        NSLog(errorDesc);
        [errorDesc release];
    }
    self.personName = [temp objectForKey:@"Name"];
    self.phoneNumbers = [NSMutableArray arrayWithArray:[temp objectForKey:@"Phones"]];
    NSLog(@"Label executed %@",[phoneNumbers objectAtIndex:0]);

    UILabel *name = [[UILabel alloc]initWithFrame:CGRectMake(-180, 100, 100, 20)];
    name.text = [phoneNumbers objectAtIndex:1];
    [self addChild:name];
    NSLog(@"Label executed %@",name);
4

4 回答 4

1

您的 plist XML 看起来有点可疑。您拥有的<array/>标签只是一个独立的标签,我不确定它是否会做任何有用的事情。

也许将 plist 更改为如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Name</key>
    <string>abcxxx</string>
    <key>Phones</key>
    <array>
        <string>121327777</string>
        <string>333333333</string>
    </array>
</dict>
</plist>

我不确定这是否会对您的代码有所帮助。您是否已通读此文档?

http://developer.apple.com/documentation/Cocoa/Conceptual/PropertyLists/Introduction/Introduction.html

于 2009-05-07T04:29:16.653 回答
0

很可能您根本无法阅读 plist,只需检查您的 plist 结构,

 NSString *StringsFromPList = [[NSBundle mainBundle] bundlePath];
 NSString *itemPositionPlistLocation = [StringsFromPList stringByAppendingPathComponent:@"Words.plist"];
 _myDictionary= [[NSDictionary alloc] initWithContentsOfFile:itemPositionPlistLocation];
NSArray * items = [_myDictionary objectForKey:@"Root"];


<plist version="1.0">
<dict>
<key>Root</key>
    <array>
        <string>Happy</string>
        <string>Rain</string>
    </array>

希望能帮助到你 :)

于 2013-06-13T21:03:45.170 回答
0

你所要做的就是调用 NSDictionary 的 initWithContentsOfFile: 方法。例如

NSDictionary *temp = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
self.personName = [temp objectForKey:@"Name"];
self.phoneNumbers = [[temp objectForKey:@"Phones"] mutableCopy];

但是,如上所述,您的 plist 看起来确实有点可疑,请尝试将其更改为 Andy White 提供的那个。

问候,

凯尔

于 2009-05-07T04:40:49.327 回答
0

UITextField 的框架也很奇怪(取决于自己的框架)CGRectMake(-180, 100, 100, 20)可能在屏幕外。

于 2009-05-07T05:10:48.083 回答