开发者

Issue loading plist into NSArray on ViewDidLoad

I'm trying to load a simple plist file in XCode 4 for an iPad application. The goal being that it will be loaded into the table portion of a split-view.

My project structure and plist look as follows:

Issue loading plist into NSArray on ViewDidLoad

I then declare an array in the interface of the RootViewController:

#import <UIKit/UIKit.h>

@class DetailViewController;

@interface RootViewController : UITableViewController {
    NSArray *sites;
}

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

@end

I then, on viewDidLoad, attempt to load in the plist:

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

    NSBundle *bundle = [NSBundle mainBundle];
    //NSLog(bundle开发者_C百科);
    NSString *path = [bundle pathForResource:@"StoredSites" ofType:@"plist"];
    NSLog(path);

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

}

The NSLog statements first return what appears to be the valid path, but upon try to load the NSArray with the contents of the file, it comes back with a count of 0:

Issue loading plist into NSArray on ViewDidLoad

I checked in the build phases to see if the file reference appears, and it seems to be ok:

Issue loading plist into NSArray on ViewDidLoad

I know this must be a simple problem, but for some reason I'm not seeing what's missing!

I appreciate the help -


You may want to using an NSDictionary, as that is what the sample code given here for the method pathForResource:ofType: uses.

I'm not entirely sure why it isn't working with an NSArray, but maybe the plist isn't formatted entirely correctly? The documentation for NSArray says that initWithContentsOfFile: takes in as a parameter quote:

The path to a file containing a string representation of an array produced by the writeToFile:atomically: method.

Again, I'm not really sure. Hope that Helps!


The problem was the plist XML was wrapped in a dictionary rather than just being a simple array of arrays -

DShah covers it here: plist in xcode creation problem

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜