我有一个这样的结构:
<?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>1</key>
<array>
<string>George Washington</string>
<string>February 22, 1732 – December 14, 1799</string>
<string>Westmoreland, Virginia</string>
<string>April 30, 1789 – March 4, 1797</string>
<string>Non-partisan</string>
</array>
<key>2</key>
<array>
<string>John Adams</string>
<string>October 30, 1735 – July 4, 1826</string>
<string>Braintree, Massachusetts</string>
<string>March 4, 1797 – March 4, 1801</string>
<string>Federalist</string>
</array>
<key>3</key>
<array>
<string>Thomas Jefferson</string>
<string>April 13, 1743 – July 4, 1826</string>
<string>Shadwell, Virginia</string>
<string>March 4, 1801 – March 4, 1809</string>
<string>Democratic-Republican</string>
</array>
</dict>
</plist>
我将这个文件放在一个名为filePath的变量中。
假设我想从plist中取出数组1,我怎样才能从中获取NSArray?
编辑:同样,我想从plist中的特定数组创建一个数组。
答案 0 :(得分:3)
当您在字典中阅读时,它包含的数组就是为您创建的。您只需要通过使用适当的密钥请求-objectForKey:
来访问它们。
NSDictionary *dictionary = [NSDictionary dictionaryWithContentsOfFile:filePath];
NSArray *georgeWashingtonInfo = [dictionary objectForKey:@"1"];
NSArray *johnAdamsInfo = [dictionary objectForKey:@"2"];
NSArray *thomasJeffersonInfo = [dictionary objectForKey:@"3"];
答案 1 :(得分:2)
所以你想把这个plist的内容读成一个数组。这很容易做到。从本地捆绑包中获取plist文件。创建一个空数组。在其中插入值。
// Path to the plist (in the application bundle)
NSString *path = [[NSBundle mainBundle] pathForResource:
@"DrinkArray" ofType:@"plist"];
NSDictionary *dictionary = [NSDictionary dictionaryWithContentsOfFile:path];
// Show the values
for (id key in dictionary)
{
if(key == 1) //gives array1
NSLog(@"bundle: key=%@, value=%@", key, [dictionary objectForKey:key]);
}