【问题标题】:Can not read plist file?无法读取 plist 文件?
【发布时间】:2014-06-12 09:35:33
【问题描述】:

我遇到了一个突然出现的恼人错误。我想读一个 .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>

谢谢:)。

【问题讨论】:

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


    【解决方案1】:

    尝试使用这个..

    NSDictionary *dictRoot = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"file_name" ofType:@"plist"]];

    NSArray *arrRoot = [NSArray arrayWithArray:[dictRoot objectForKey:@"Root"]];

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 2011-09-06
        • 2012-02-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多