fun*_*der 6 iphone plist nsarray nsbundle
我有一个包含2个字符串的数组的plist看到图像:
http://imageshack.us/photo/my-images/263/myplist.png/
在我的viewDidLoadi中添加:
NSString *file = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"];
NSArray *array = [NSArray arrayWithContentsOfFile:file];
NSLog(@"array = %@", array);
Run Code Online (Sandbox Code Playgroud)
但我总是null为什么?感谢帮助.
更新:解决方案是你需要手动编辑plist文件(Xcode4默认使用Dictionnary)
<?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>
<array>
<string>HO</string>
<string>HI</string>
</array>
</array>
</plist>
Run Code Online (Sandbox Code Playgroud)
omz*_*omz 13
很可能,你的plist是一个包含数组的字典(Xcode 4不允许创建以数组作为根对象的plists).尝试这样的事情:
NSString *file = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"];
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:file];
NSArray *array = [dict objectForKey:@"Array"];
NSLog(@"%@", array);
Run Code Online (Sandbox Code Playgroud)