数组不从plist加载数据

发布于 2024-10-31 03:41:39 字数 1202 浏览 5 评论 0原文

我不确定我的问题到底是什么,因为我已经使用这种方法有一段时间了,但从未遇到过这个问题。

基本上就如标题所说。此代码从位于它显示的“资源”组中的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

橙味迷妹 2024-11-07 03:41:39

请改用字典。我对 NSArrays 和属性列表有未定义的行为。例如

NSString *myFile = [[NSBundle mainBundle] pathForResource:@"myList" ofType:@"plist"];
NSMutableDictionary* myDict = [[NSMutableDictionary alloc]initWithContentsOfFile:myFile];
NSLog(@"%@", myDict);

Use a dictionary instead. I've had undefined behavior with NSArrays and property lists. e.g.

NSString *myFile = [[NSBundle mainBundle] pathForResource:@"myList" ofType:@"plist"];
NSMutableDictionary* myDict = [[NSMutableDictionary alloc]initWithContentsOfFile:myFile];
NSLog(@"%@", myDict);
违心° 2024-11-07 03:41:39

我也有类似的情况。解决:

  1. 在树
  2. 调用上下文菜单
  3. 中选择 *.plist选择“使用外部编辑器打开”
  4. 根将可见 - 将“Class”更改为“Array”
  5. 保存

I had a similar situation. Solving:

  1. select *.plist in tree
  2. call context menu
  3. select "Open with external editor"
  4. root will be visible - change "Class" to "Array"
  5. save
不可一世的女人 2024-11-07 03:41:39

你所拥有的看起来不错。检查文件是否位于正确的目录中并且包含内容。可能需要在执行此操作时 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文