在我的应用程序中,我将一些信息存储在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 €</string>
</dict>
</array>
</plist>
我的解决方案有什么问题?为什么我无法读取plist文件的内容?谢谢!
答案 0 :(得分: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"];