2
我有20個詞典在plist文件中。我需要用他們的名字編輯一個特定的詞典,但是真的不知道如何做到這一點。 我需要獲取值並對SearchCountry字典進行更改。如何在plist文件中編輯特定的NSDictionary?
如何編輯特定字典值?
我有20個詞典在plist文件中。我需要用他們的名字編輯一個特定的詞典,但是真的不知道如何做到這一點。 我需要獲取值並對SearchCountry字典進行更改。如何在plist文件中編輯特定的NSDictionary?
如何編輯特定字典值?
得到一個子字典是這樣的:
NSMutableDictionary *plist = [[NSMutableDictionary alloc] initWithContentsOfFile:plistFilename];
NSMutableDictionary *searchCountry = [plist objectForKey:@"SearchCountry"];
更改它的一個這樣的價值觀:
[searchCountry setValue:@"Cleveland" forKey:@"searchcity"];
保存如下:
BOOL success = [plist writeToFile:plistFilename atomically:YES];
嘗試使用NSPorpertyListSerialization的財產名單上覆雜的迭代。
否則,你可能試圖訪問特定字典使用下面的代碼
NSString* plistPath = [[NSBundle mainBundle] pathForResource:@"yourPList"
ofType:@"plist"];
NSDictionary* plistDict = [NSDictionary dictionaryWithContentsOfFile:plistPath];
// plistDict contains all the dictionaries inside it now, you could try to access the particular inside the plistDict
id *searchKey = [plistDict objectForKey:@"SearchCountry"];
//OR
NSString* searchKeys = [plistDict valueForKeyPath:@"SearchCountry"];
它的工作就像魅力。謝謝丹 – GoCrazy 2013-03-26 00:12:46