从 plist 获取数据到 NSMutableArray 并将 NSMutableArray 写入同一 plist iPhone
使用此代码,我尝试从 Templates.plist 文件获取数据,但我在数组中获取空值。
//from plist
NSString *path = [[NSBundle mainBundle] pathForResource:@"Templates" ofType:@"plist"];
NSMutableArray *arry=[[NSMutableArray alloc]initWithContentsOfFile:path];
NSLog(@"arrayArray:::: %@", arry);
这是我的 plist 文件:
另外我想知道如何向此 plist 文件添加更多字符串并删除此 plist 中的字符串。
Using this code I am trying to get the data from Templates.plist file but I am getting null value in array.
//from plist
NSString *path = [[NSBundle mainBundle] pathForResource:@"Templates" ofType:@"plist"];
NSMutableArray *arry=[[NSMutableArray alloc]initWithContentsOfFile:path];
NSLog(@"arrayArray:::: %@", arry);
This is my plist file:
Also I want to know how can I add more strings to this plist file and delete a string from this plist.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先,你不能写入 mainBundle 中的任何内容。为了写入 plist,您需要将其复制到文档目录。这样做是这样的:
所以调用这个函数将检查文件是否存在于文档目录中。如果没有,它将文件复制到文档目录。如果文件存在它就返回。接下来,您只需访问该文件即可对其进行读取和写入。要访问,您只需要文档目录的路径并将文件名添加为路径组件。
从 plist 获取数据:
将文件写回文档目录。
First off you cannot write to anything in you mainBundle. For you to write to you plist you need to copy it to the documents directory. This is done like so:
So calling this function will check if the file exists in the documents directory. If it doesn't it copies the file to the documents directory. If the file exists it just returns. Next you just need to access the file to be able to read and write to it. To access you just need the path to the documents directory and to add your file name as a path component.
To get the data from the plist:
To write the file back to the documents directory.
要将 plist 放入可变数组中,请使用此类方法:
然后,从数组中添加/删除字符串。要将数组保存回 plist,请使用:
To get a plist into a mutable array, use this class method:
Then, add/delete strings from the array. To save the array back to a plist, use: