阅读 写入 plist Iphone
我已经看到了很多与此相关的问题,但似乎找不到适合我需要的问题。
我有一个看起来像这样的 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>Content</key>
<array>
<dict>
<key>dateAdd</key>
<date>2011-03-23T14:40:47Z</date>
<key>content</key>
<string>Content first</string>
<key>Title</key>
<string>Title 1</string>
</dict>
<dict>
<key>dateAdd</key>
<date>2011-03-23T14:40:47Z</date>
<key>content</key>
<string>Content here</string>
<key>Title</key>
<string>Title 2</string>
</dict>
</array>
</dict>
</plist>
我从 plist 中获取信息,如下所示
在第一个类中
//points to the plist
NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *url = [path stringByAppendingPathComponent:@"Details.plist"];
//filling with contents of plist
NSDictionary *Data = [[NSDictionary alloc] initWithContentsOfFile:url];
self.contentData = Data;
[Data release];
在第二个类中,我创建此类的一个实例并获取信息
Petite_NotesAppDelegate *instance = (Petite_NotesAppDelegate *)[[UIApplication sharedApplication] delegate];
self.dataArray = [instance.contentData objectForKey:@"Content"];
这使得可以从 plist 中获取内容,如下所示
NSDictionary *Content = [self.dataArray objectAtIndex:indexPath.row];
cell.textLabel.text = [Content objectForKey:@"Title"];
My问题是我无法写入 plist
因为我想写入的内容位于 plist 数组 Content 中,然后位于 item 0 item 1 等内部。我如何写入 plist 中的 content 数组内部?假设我想从文本字段写入内容...
感谢您的帮助!
I have seen quite a few questions about this but cant seem to find a one that fits my needs.
I have a plist that looks like this
<?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>Content</key>
<array>
<dict>
<key>dateAdd</key>
<date>2011-03-23T14:40:47Z</date>
<key>content</key>
<string>Content first</string>
<key>Title</key>
<string>Title 1</string>
</dict>
<dict>
<key>dateAdd</key>
<date>2011-03-23T14:40:47Z</date>
<key>content</key>
<string>Content here</string>
<key>Title</key>
<string>Title 2</string>
</dict>
</array>
</dict>
</plist>
Im fetching the information from the plist like this
In the first class
//points to the plist
NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *url = [path stringByAppendingPathComponent:@"Details.plist"];
//filling with contents of plist
NSDictionary *Data = [[NSDictionary alloc] initWithContentsOfFile:url];
self.contentData = Data;
[Data release];
In the second class i create an instance of this class and fetch the information
Petite_NotesAppDelegate *instance = (Petite_NotesAppDelegate *)[[UIApplication sharedApplication] delegate];
self.dataArray = [instance.contentData objectForKey:@"Content"];
This makes it possible to get the content from the plist like this
NSDictionary *Content = [self.dataArray objectAtIndex:indexPath.row];
cell.textLabel.text = [Content objectForKey:@"Title"];
My problem is i am not able to write to the plist
Since the content i want to write is in the plist array Content and then inside the item 0 item 1 etc. How would i write to inside the content array in the plist? Lets say i want to write content from a textfield...
Thanks for any help!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
问题在于您可能正在尝试写入与读取的同一个文件。恐怕这是不可能的 - 您正在从只读的应用程序包中读取内容。如果要将更改保存到磁盘,则需要将字典写入位于 Documents 目录中的 plist。
要获取 Documents 目录的路径,您需要执行以下操作(还有其他方法可以执行此操作):
这也意味着当您启动应用程序时,您应该检查 Details.plist 文件是否存在在您的文档目录中,如果存在,请从中读取,否则退回到从应用程序包中读取。
The problem is that you are probably trying to write to the same file that you read from. I'm afraid that's not possible - you are reading from the application bundle which is read only. If you want to save your changes to disk you need to write your dictionary to a plist located in the Documents directory.
To get the path to the Documents directory you need to do the following (there are other ways of doing this as well):
This also means that when you start your application you should check to see if the Details.plist file exists in your Documents directory, if it does, read from it, otherwise fall back to reading from the the application bundle.
您可以对字典进行更改,然后使用
writeToFile:
示例
You can make changes to the dictionary and then use
writeToFile:
Sample
这可以通过 deepcopy 数组轻松处理
This can easly handle by deepcopy array