2011-06-02 81 views
6

我有一个数组的plist包含2串看到图像:arrayWithContentsOfFile无法读取plist文件

http://imageshack.us/photo/my-images/263/myplist.png/

,并在我的viewDidLoad我补充一点:

NSString *file = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"]; 

NSArray *array = [NSArray arrayWithContentsOfFile:file]; 

NSLog(@"array = %@", array); 

但我为什么总是得到null?感谢帮助。

UPDATE:解决的办法是,你需要手动编辑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> 
+0

发布你的完整代码.. – Aravindhan 2011-06-02 04:49:00

+0

@funnyCoder你检查路径是否存在?也许在那里贴一个[[[NSFileManager defaultManager] fileExistsAtPath:file]'? – 2011-06-02 04:51:24

+0

@Aravindhanarvi这是我的完整代码:)。 – funnyCoder 2011-06-02 04:52:40

回答

13

最有可能的,你的plist是包含数组的字典(4 Xcode中不允许创建一个数组作为根对象)。尝试是这样的:

NSString *file = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"]; 
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:file]; 
NSArray *array = [dict objectForKey:@"Array"]; 
NSLog(@"%@", array); 
+0

谢谢,但总是空! – funnyCoder 2011-06-02 04:55:56

+0

对不起,谢谢你没关系:)。但你能解释为什么这样吗? – funnyCoder 2011-06-02 04:57:11

+0

@omz老兄它允许创建实际上允许创建数组作为根 – NIKHIL 2011-06-02 04:59:30

0

plist should start with Root Key

在你的应用程序,你需要更改根密钥名称,因为它不能是数据类型

+0

我改变了它,但没有成功,似乎Xcode 4不允许数组plist ?! – funnyCoder 2011-06-02 05:01:55

+0

Ohhh是的,它就是这样 – NIKHIL 2011-06-02 05:05:37

+0

所以hiw来处理?你可以给我一个关于Xcode4 Array plist支持的网址吗?谢谢。 – funnyCoder 2011-06-02 05:10:32

0

我已经研究过这个话题,发现在一些差异plist文件。

例如,我在样本代码的原始plist文件被读取与 方法
NSArray *array = [[NSArray alloc] initWithContentsOfFile:path];

在xml原始文件是:

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" 
"http://www.apple.com/DTDs/PropertyList-1.0.dtd"> 

<plist version="1.0"> 

<array> 
<dict> 
    <key>nameKey</key> 
    <string>Number One</string> 
    <key>imageKey</key> 
    <string>small_one.png</string> 
</dict> 
<dict> 
    <key>nameKey</key> 
    <string>Number Two</string> 
    <key>imageKey</key> 
    <string>small_two.png</string> 
</dict> 

</array>   
</plist> 

当我试图创建此相同上面的文件在Xcode界面中,使用添加新的文件属性列表,并且在编辑后将该项目以同样的方式显示在上述文件的界面中,则方法[[NSArray alloc] initWithContentsOfFile:path];失败,并且我在xml plist文件的结构中发现了该问题。 使用的XCode接口创建的文件,导致XML plist文件:

<plist version="1.0"> 
<dict> 
<key>item 0</key> 
<dict> 
    <key>imageKey</key> 
    <string>image1.jpg</string> 
</dict> 
<key>item 1</key> 
<dict> 
    <key>imageKey</key> 
    <string>image2.jpg</string> 
</dict> 
</dict> 
</plist> 

我固定更改xml文件直接作为相同的第一个例子,该方法工作得很好。

点是在plist文件的XCode接口,你不能看到这些差异。两者看起来都一样。也许是我的Xcode 4.1

0

正确的答案是,根项目已经是一个数组(在XCode4 ..不知道其他人)。所以如果你把另一个数组作为neil d显示在他的屏幕截图中,你最终会得到一个包含两个字符串值的数组的数组。

0

这种类型的plist

enter image description here

OR(基本上都是相同的)

enter image description here

可以这样写:

NSString *file = [[NSBundle mainBundle] pathForResource:@"appFeatures" ofType:@"plist"]; 
_arrFeats = [NSArray arrayWithContentsOfFile:file]; 
NSLog(@"%i", _arrFeats.count); 

appFeaturesplist的名称。 (iOS 6.1)