1

我有一个包含字典数组的文件。如何将其读回以将其用作在我的代码中包含这些字典的常规数组?

<?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">
<array>
    <dict>
        <key>elem1</key>
        <string>Hello world</string>
        <key>elem2</key>
        <string>This is some string</string>
    </dict>
</array>
</plist>
4

2 回答 2

1

这实际上是相当简单的。感谢H2CO3指向它。只需使用 [NSArray arrayWithContentsOfFile:] 方法。所以这就是我所拥有的:

// Path to my file
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString  *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"my_file.out"];

// Get the array contained in the file
NSMutableArray *myArray = [NSMutableArray arrayWithContentsOfFile:filePath];

// Read the array's content
int numOfelems = [myArray count];
for (int i = 0; i < numOfelems; i++) {
    NSLog(@"Object at index %i: %@", i, [myArray objectAtIndex:i]);
}
于 2013-01-02T00:28:55.037 回答
1

这很简单:可以在一行中解析属性列表...

NSArray *array = [NSArray arrayWithContentsOfFile:pathToPLIST];
于 2013-01-02T00:31:49.087 回答