[英]iPhone Dev - Edit objects in Array from Plist
我有一個Plist,其中有多個Array。 在這些數組中,我正在讀取不同的對象,但是在編輯數組中的這些對象然后將數組再次保存回Plist文件時,我陷入了困境。
我已經像這樣讀取文件了...
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文件中。
提前致謝!
PS-如果需要,我願意發布更多代碼或信息。
您不能將數據保存在應用程序的主捆綁包中,而必須在文檔目錄中進行如下操作:
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];
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.