数组不从plist加载数据
我不确定我的问题到底是什么,因为我已经使用这种方法有一段时间了,但从未遇到过这个问题。
基本上就如标题所说。此代码从位于它显示的“资源”组中的 plist 加载数据
NSString *myFile = [[NSBundle mainBundle] pathForResource:@"myList" ofType:@"plist"];
myArray = [[NSArray alloc]initWithContentsOfFile:myFile];
NSLog(@"%@", myArray);
(空)。有人知道为什么吗?
我在同一个 ViewController 中有另一个设置并且工作得很好。但是,这个特定的 plist 不会加载。我什至还删除了 plist,并从另一个 plist 创建了一个副本,以确保不是结构问题。
我这样做的方式错误吗?我应该使用文件管理器吗?
更新 - - - - - - - - - - - - - - - - - - - - - - - - - ------------------------
好吧,我简直不敢相信我要说的话。哈哈,
我使用的是 XCode 4,在 Xcode 中编辑 plist 的方式与 Xcode 3.x 不同。正如在早期版本中一样,您可以选择您想要的 plist 的根(数组、字典等)。我的问题是,我试图加载到 NSArray 中的 plist 实际上是结构化为字典。但是我从未在 Xcode 中看到过这一点,除了使用 TextEdit 或其他文本编辑器浏览内容之外,我不知道如何看到这一点。
我必须说很愚蠢。有谁知道为什么 XCode 4 不会像 XCode 3.x 那样显示 plist 的根目录?
XCode 3.x:http://dl.dropbox.com/u/919254/XCode3。 png
XCode 4:http://dl.dropbox.com/u/919254/Screen%20shot%202011-04-10%20at%202.30.35%20AM.png
I'm not sure what exactly is my problem since I have been using this method for a while now and never had this problem.
Basically, as the title says. This code loads data from a plist located in the "Resources" group
NSString *myFile = [[NSBundle mainBundle] pathForResource:@"myList" ofType:@"plist"];
myArray = [[NSArray alloc]initWithContentsOfFile:myFile];
NSLog(@"%@", myArray);
It displays (null). Anyone know why?
I have in the same ViewController another one setup and works just fine. This particular plist does not load however. And I've also even deleted the plist and just created a duplicate from the other one to make sure is not a structure issue.
I am doing this the wrong way? Should I use FileManager?
UPDATE -------------------------------------------------------------------------
Ok so, I can't believe what I am about to say. lol
I am using XCode 4, the way you edit plists in Xcode is different from Xcode 3.x. As in the earlier versions you get to choose the root of the plists to be w/e you want them to (Array, Dictionary, etc) My problem was that the plist I was trying to load into a NSArray was in fact structured as a dictionary. However I never saw this in Xcode, and I have no idea how to see this other than browsing the contents using TextEdit or some other text editor.
Pretty silly I must say. Does anyone know why XCode 4 won't show the root of the plist the way XCode 3.x does?
XCode 3.x: http://dl.dropbox.com/u/919254/XCode3.png
XCode 4: http://dl.dropbox.com/u/919254/Screen%20shot%202011-04-10%20at%202.30.35%20AM.png
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
请改用字典。我对 NSArrays 和属性列表有未定义的行为。例如
Use a dictionary instead. I've had undefined behavior with NSArrays and property lists. e.g.
我也有类似的情况。解决:
I had a similar situation. Solving:
你所拥有的看起来不错。检查文件是否位于正确的目录中并且包含内容。可能需要在执行此操作时 NSLog myFile。
What you have looks right. Check that the file is in the right directory, and has contents. Might want to NSLog myFile while your at it.