2011-08-27 76 views
1

我試圖在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

回答

1

您可能需要使用NSDictionary,因爲這是示例代碼給出here的方法pathForResource:ofType:用途是什麼。

我不完全確定它爲什麼不能使用NSArray,但也許plist格式不正確?該documentation for NSArrayinitWithContentsOfFile:發生在作爲參數報價:原子:方法

到含有由將writeToFile產生的陣列 的字符串表示的文件的路徑。

再一次,我不太確定。希望有助於!