关于XCode 4中读取Plist文件是本文要介绍的内容,主要介绍的是plist文件图和来获取,内容不多,主要是通过代码来实现。不多说,先来看详细内容。
1. 添加plist文件
2. 代码读取
- // 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);
其中array2总是为空,原因:
选中DrinkArray.plist,ctrl + click(或右键,如果有鼠标) open as->source code, 结果如下:
- <?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>