1

在我的应用程序中,我将一些信息存储在 plist 文件中。我用这段代码生成了这个 plist:

- (void)saveArrayToFile {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *fileName = [documentsDirectory stringByAppendingPathComponent:@"objects.plist"];
    [self.reportArray writeToFile:fileName atomically:YES];
}

使用应用程序 iExplorer,我查看了我创建的应用程序以找到我创建的文件,现在我需要读回我的文件。我在网上找到了几个允许我读取这个 plist 文件的解决方案,所以我尝试了这个解决方案:

- (void)loadObjectFromFile {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
    NSString *documentsDir = [paths objectAtIndex:0];
    NSString *path = [documentsDir stringByAppendingString:@"objects.plist"];
    NSArray *temp = [NSArray arrayWithContentsOfFile:path];
    NSLog(@"%@", temp);
}

但是如果我在 Xcode 中查看控制台,我看不到我的 plist 文件内容的日志,我得到了这个:

2013-10-17 10:29:34.586 MyApp[233:60b] (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">
<array>
    <dict>
        <key>desc</key>
        <string>Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Nam liber te conscient to factor tum poen legum odioque civiuda.</string>
        <key>name</key>
        <string>Dadi rossi</string>
        <key>price</key>
        <string>5 €&lt;/string>
    </dict>
</array>
</plist>

我的解决方案有什么问题?为什么我无法读取我的 plist 文件的内容?谢谢!

4

1 回答 1

2

你的错误在第一行阅读

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);

NSDocumentationDirectory在将数据存储在NSDocumentDirectory. 只需用以下行替换它。

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

此处的第二个错误附加字符串而不是 PathComponent 替换

NSString *path = [documentsDir stringByAppendingString:@"objects.plist"];

NSString *fileName = [documentsDirectory stringByAppendingPathComponent:@"objects.plist"];
于 2013-10-17T08:53:51.040 回答