如何阅读和阅读写入 NSArray 到 plist?
我遇到了一些关于从 plist 读取和写入 NSArray
的问题。
我在“支持文件”文件夹中创建了一个 plist 文件,我想用它在第一次加载时初始化应用程序数据。
这是我的 plist 的样子:
然后我使用此代码尝试将 plist 加载到应用程序中:
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
filePath = [documentsDirectory stringByAppendingPathComponent:kDataFile];
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:filePath])
{
NSString *bundle = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"];
[fileManager copyItemAtPath:bundle toPath:filePath error:&error];
}
然后我尝试像这样从 plist 文件加载数据,但似乎没有显示任何内容。
NSMutableDictionary *savedData = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];
NSMutableArray *myNSArray = [[savedData objectForKey:@"KEY_Level_1"] mutableCopy];
savedData = nil;
抱歉,如果这是一个简单的任务,但是我一直在查看大量教程并尝试找出如何做到这一点,但没有运气。我现在真的很沮丧——我本以为这应该是一件简单的事情。
注意: 我的 NSArray
将包含一大堆 NSDictionaries。
I'm having several issues based around reading and writing an NSArray
to and from a plist.
I have created a plist file in the 'Supporting Files' folder which I want to use to initialise the app data with upon the first load.
Here is what my plist looks like:
I then use this code to try load the plist into the app:
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
filePath = [documentsDirectory stringByAppendingPathComponent:kDataFile];
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:filePath])
{
NSString *bundle = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"];
[fileManager copyItemAtPath:bundle toPath:filePath error:&error];
}
I then try to load the data from the plist file like so, however nothing seems to be displayed.
NSMutableDictionary *savedData = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];
NSMutableArray *myNSArray = [[savedData objectForKey:@"KEY_Level_1"] mutableCopy];
savedData = nil;
Sorry if this is a simple task, however I've been looking at lots of tutorials and trying to work out how to do this with no luck. I'm getting really frustrated now - I would have thought it should be a simple thing to do.
NOTE: My NSArray
will contain a whole bunch of NSDictionaries.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要检查
copyItemAtPath:toPath:error:
的返回值,如果该方法返回 false,至少记录错误:-[NSDictionary initWithContentsOfFile:]
无法报告错误,因此如果失败,您无法轻易找出原因。尝试将文件读入NSData
并使用-[NSPropertyListSerialization propertyListWithData:options:format:error:]
解析它:如果这样做,您将能够更容易了解为什么您的数据没有被加载。
更新
经过进一步检查,您的 plist 中的顶级字典似乎包含键“Root”。 “Root”的值是包含键“KEY_Level_1”的字典。所以你需要这样做:
You need to check the return value of
copyItemAtPath:toPath:error:
and at least log the error if the method returns false:-[NSDictionary initWithContentsOfFile:]
has no way to report errors, so if it's failing, you cannot easily figure out why. Try reading the file into anNSData
and using-[NSPropertyListSerialization propertyListWithData:options:format:error:]
to parse it:If you do this, you'll be able to more easily see why your data is not being loaded.
UPDATE
On further inspection, it looks like the top-level dictionary in your plist contains the key "Root". The value for "Root" is a dictionary containing the key "KEY_Level_1". So you need to do this: