无法读取plist文件?

时间:2014-06-12 09:35:34

标签: objective-c ipad ios7 plist xcode5.1

我有一个恼人的错误突然来了。我想读一个.plist文件。但目前还不可能。它工作在昨天,甚至3小时前,然后我的应用程序在模拟器和设备上崩溃。我已经听过这个教程(两次)(和其他人):

https://www.youtube.com/watch?v=3Oy-evy2xD8

用Google搜索了许多stackoverflow解决方案(除了这两个......):

Plist cannot be read ... What am I doing wrong?

Can't read from plist

但我仍然没有更接近灵魂。

NSError *error;
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *documentDirectory = [path objectAtIndex:0];

documentDirectory = [documentDirectory stringByAppendingPathComponent:@"testPlist.plist"];


NSLog(@" documentDirectory: - %@ ",documentDirectory);

if ([[NSFileManager defaultManager] fileExistsAtPath:documentDirectory])
{
    NSLog(@" The file exits! ! !");
}
else
{
    NSLog(@" The file DOES NOT exits !!!");
}

NSFileManager *fileManager = [NSFileManager defaultManager];

if(![fileManager fileExistsAtPath: documentDirectory]) {
    NSString *bundle = [[NSBundle mainBundle] pathForResource:@"testPlist" ofType:@"plist"];

    [fileManager copyItemAtPath:bundle toPath:documentDirectory error:&error];

    NSLog(@"plist is copied to Directory");
}


NSDictionary * plistDictionary = [[NSDictionary alloc] initWithContentsOfFile:documentDirectory];
NSMutableDictionary * plistMutableDictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:documentDirectory];
NSArray *items = [plistDictionary objectForKey:@"Root"];

//NSMutableArray *itemsFromMutableDict = [[NSMutableArray alloc] init];
//[itemsFromMutableDict addObject:[plistDictionary objectForKey:@"Root"]];

NSLog(@" Path : %@ ",documentDirectory);
NSLog(@" Items in array = %@", items);
NSLog(@" array count = %i", [items count]);
NSLog(@" Why is this %@ ?",[plistDictionary objectForKey:@"Root"]);

输出为:

2014-06-12 11:24:25.894 PlistTestingProject[1879:60b]  All paths : /Users/webbite_mladen/Library/Application Support/iPhone Simulator/7.1/Applications/A1A7D9E3-BFDE-4C52-8C5F-28F0D88D6F5F/Documents 

2014-06-12 11:24:25.896 PlistTestingProject[1879:60b]  documentDirectory: - /Users/webbite_mladen/Library/Application Support/iPhone Simulator/7.1/Applications/A1A7D9E3-BFDE-4C52-8C5F-28F0D88D6F5F/Documents/testPlist.plist 

2014-06-12 11:24:25.897 PlistTestingProject[1879:60b]  The file exits! ! !

2014-06-12 11:24:25.897 PlistTestingProject[1879:60b]  Path : /Users/webbite_mladen/Library/Application Support/iPhone Simulator/7.1/Applications/A1A7D9E3-BFDE-4C52-8C5F-28F0D88D6F5F/Documents/testPlist.plist 

2014-06-12 11:24:25.898 PlistTestingProject[1879:60b]  Items in array = (null)

2014-06-12 11:24:25.898 PlistTestingProject[1879:60b]  array count = 0

2014-06-12 11:24:25.899 PlistTestingProject[1879:60b]  Why is this (null) ?

似乎我无法访问plist文件。

plist文件内容:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
    <dict>
        <key>Root</key>
        <array>
            <string>sunday</string>
            <string>monday</string>
            <integer>44</integer>
        </array>
    </dict>
</plist>

谢谢:)。

2 个答案:

答案 0 :(得分:0)

尝试使用此..

NSDictionary * dictRoot = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@&#34; file_name&#34; ofType:@&#34; plist中&#34;]];

NSArray * arrRoot = [NSArray arrayWithArray:[dictRoot objectForKey:@&#34; Root&#34;]];

答案 1 :(得分:0)

最简单的解决方案终于奏效了:

NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"myPlistFile"
                                                              ofType:@"plist"];
NSMutableArray *myArray = [NSMutableArray arrayWithContentsOfFile:plistPath];