问题在ViewDidLoad中加载plist到NSArray

问题描述:

我试图在iPad应用程序的XCode 4中加载一个简单的plist文件。目标是将它加载到分割视图的表部分。问题在ViewDidLoad中加载plist到NSArray

我的项目结构和plist中如下所示:

#import <UIKit/UIKit.h> 

@class DetailViewController; 

@interface RootViewController : UITableViewController { 
    NSArray *sites; 
} 

@property (nonatomic, retain) IBOutlet DetailViewController *detailViewController; 

@end 

我的话,在viewDidLoad中,试图加载:

enter image description here

然后我在RootViewController的界面声明数组在plist中:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    self.clearsSelectionOnViewWillAppear = NO; 
    self.contentSizeForViewInPopover = CGSizeMake(320.0, 600.0); 

    NSBundle *bundle = [NSBundle mainBundle]; 
    //NSLog(bundle); 
    NSString *path = [bundle pathForResource:@"StoredSites" ofType:@"plist"]; 
    NSLog(path); 

    sites = [[NSArray alloc] initWithContentsOfFile:path]; 
    NSLog(@"%d", [sites count]); 

} 

的NSLog的STA tements先回似乎是有效的路径,但在尝试用文件的内容加载的NSArray,它的0计数回来:

enter image description here

我在构建阶段检查看是否出现的文件引用,它似乎是确定:

enter image description here

我知道这一定是一个简单的问题,但由于某种原因,我没有看到有什么遗漏!

我欣赏的帮助 -

+0

一些什么类似的问题在我以前的帖子讨论,请[经过链接] [1] [1]:http://stackoverflow.com/questions/6693333/plist-in-xcode-creation-problem – DShah

+0

那是我的确切问题,根节点是包装在字典中 - 感谢您的链接!我upvoted所有的答案:) – Evan

+0

总是欢迎....... – DShah

的问题plist中XML被包裹在一本字典,而不是仅仅是数组的简单数组 -

DShah涵盖在这里:plist in xcode creation problem

您可能需要使用NSDictionary,因为这是示例代码给出here的方法pathForResource:ofType:用途是什么。

我不完全确定它为什么不能使用NSArray,但也许plist格式不正确?该documentation for NSArrayinitWithContentsOfFile:发生在作为参数报价:原子:方法

到含有由将writeToFile产生的阵列 的字符串表示的文件的路径。

再一次,我不太确定。希望有助于!