iPhone Dev - Edit objects in Array from Plist(iPhone Dev - 从 Plist 编辑数组中的对象)
问题描述
我有一个 Plist,其中有许多 Array.在这些数组中是我正在读取的不同对象,但是我在编辑数组中的这些对象然后再次将数组保存回 Plist 文件时陷入困境.
I have a Plist in which I have a number of Array's. Within these arrays are different objects that I'm reading, I've become stuck however on editing these objects in the array and then saving the array back to the Plist file again.
我已经像这样读取文件了...
I'm already reading the file like so...
Array = [myArrays objectAtIndex:arrayCounter]; //This tells it which Array to load.
myObject = [Array objectAtIndex:0]; //This reads that object in the array. (This is what I want to edit.)
我想知道如何编辑该对象(它是一个字符串),然后将其保存回数组中,然后将该数组保存回存储它的 Plist 文件中.
I was wondering how I would go about editing that object (it's a string) and then saving it back into the array and then saving that array back into the Plist file where it is stored.
提前致谢!
P.S - 如果需要,我愿意发布更多代码或信息.
P.S - I'm willing to post more code or information if required.
推荐答案
你不能将数据保存在应用程序的主包中,而必须像这样在文档目录中进行:
You cannot save data in application's main bundle instead you have to do in document directory like this:
NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *plistFilePath = [documentsDirectory stringByAppendingPathComponent:@"Data.plist"];
if([[NSFileManager defaultManager] fileExistsAtPAth:plistFilePath])
{//plist already exits in doc dir so save content in it
NSMutableArray *data = [NSMutableArray arrayWithContentsOfFile:plistFilePath];
NSArray *arrValue = [data objectAtIndex:arrayCounter];
NSString *strValue = [arrValue objectAtIndex:0];
strValue = [strValue stringByAppending:@"hello"]; //change your value here
[[data objectAtIndex:arrayCounter] replaceObjectAtIndex:0 withObject:strValue]; //value replaced
[data writeToFile:plistFilePath atomically:YES];
}
else{ //firstly take content from plist and then write file document directory. it will be executed only once
NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"];
NSMutableArray *data = [NSMutableArray arrayWithContentsOfFile:plistPath];
NSArray *arrValue = [data objectAtIndex:arrayCounter];
NSString *strValue = [arrValue objectAtIndex:0];
strValue = [strValue stringByAppending:@"hello"]; //change your value here
[[data objectAtIndex:arrayCounter] replaceObjectAtIndex:0 withObject:strValue]; //value replaced
[data writeToFile:plistFilePath atomically:YES];
}
这篇关于iPhone Dev - 从 Plist 编辑数组中的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:iPhone Dev - 从 Plist 编辑数组中的对象
基础教程推荐
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01