iPhone开发 - 从plist中数组编辑对象 [英] iPhone Dev - Edit objects in Array from Plist

查看:172
本文介绍了iPhone开发 - 从plist中数组编辑对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个plist中,其中我公司拥有一批阵列的的。这些阵列内是我读,我被卡住但是对数组中编辑这些对象,然后再保存该阵列回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 - 我愿意发布更多code,或者如果需要的信息。

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开发 - 从plist中数组编辑对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆