我有一个包含字典数组的文件。我如何阅读它以将其用作我的代码中包含这些词典的常规数组?
<?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>
答案 0 :(得分: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]);
}
答案 1 :(得分:1)
这非常简单:属性列表可以在一行中解析...
NSArray *array = [NSArray arrayWithContentsOfFile:pathToPLIST];