有的书籍上关于要读取PLIST文件的说明是:先要在XCODE文件中建一个plist文件,然后把根类型改为数组,用下面的代码即可:
// Path to the plist (in the application bundle)
NSString *path = [[NSBundle mainBundle] pathForResource:
@"DrinkArray" ofType:@"plist"];
// Build the array from the plist
NSMutableArray *array2 = [[NSMutableArray allObjective-C] initWithContentsOfFile:path];
// Show the string values
for (NSString *str in array2)
NSLog(@"--%@", str);
但是在xcode4中,不能找到修改根类型的选项(如果有,烦请留言)。因为默认情况下plist是做为字典类型的,所以用上述的代码在运行时读不出文件的内容,修改方法是:要用NSDict来取代NSMutableArray。 如果一定要用数组类型,可以在plist文件上点右键,选取“打开源代码”,然后把dict标签去掉,换成<array>即可:
参见:
把
<?xml version="1.0" encoding="UTF-8"?>
<!DObjective-CTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Root</key>
<array>
<string>Firecracker</string>
<string>Lemon Drop</string>
<string>Mojito</string>
</array>
</dict>
</plist>
改为:
<?xml version="1.0" encoding="UTF-8"?>
<!DObjective-CTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
<string>Firecracker</string>
<string>Lemon Drop</string>
<string>Mojito</string>
</array>
</plist>