不是100%肯定我知道你之後,你的意思是你要編輯的密鑰字符串,或者存儲在KET字符串,我懷疑後者.. 。
NSString *origString = [plistDict objectForKey:@"Key one"];
NSString *newString = [NSString stringWithFormat:@"Some change here to string = %@",origString];
[plistDict setObject:newString forKey:@"Key one"]; // Will overwrite the original dictionary entry with the new string
行,所以根據你想改變一個字典內的字典我相信額外的信息,在這種情況下,上面的代碼需要一個小的變化...
NSMutableDictionary *subDictionary = [plistDict objectForKey:@"Key One"];
NSString *origString = [subDictionary objectForKey:@"Key one"];
NSString *newString = [NSString stringWithFormat:@"Some change here to string = %@",origString];
[subDictionary setObject:newString forKey:@"Key one"]; // Will overwrite the original dictionary entry with the new string
subDictionary = [plistDict objectForKey:@"Key Two"];
NSString *origString = [subDictionary objectForKey:@"Key one"];
NSString *newString = [NSString stringWithFormat:@"Some other change here to string = %@",origString];
[subDictionary setObject:newString forKey:@"Key one"]; // Will overwrite the original dictionary entry with the new string
這就是你所追求的?在這種情況下,我不是100%確定,但我不認爲plistDict需要被聲明爲Mutable,因爲根字典本身不會改變,只有子字典。
你當然應該驗證指針從返回[plistDict objectForKey:XXX]是不是零,以確保關鍵纔去到修改的結果確實存在......
NSMutableDictionary *subDictionary = [plistDict objectForKey:@"Key One"];
if(subDictionary!=nil)
{
NSString *origString = [subDictionary objectForKey:@"Key one"];
if(origString!=nil)
{
NSString *newString = [NSString stringWithFormat:@"Some other change here to string = %@",origString];
[subDictionary setObject:newString forKey:@"Key one"]; // Will overwrite the original dictionary entry with the new string
}
}
注意我曾經讀過在目標C的書,在目標C它不是足夠簡單地測試一個指針:
如果(子詞典) { }
如在布爾測試目標C只測試底8位表示無零值,忽略指針中的其餘位,因此如果指針位於256字節的邊界上,if實際上將返回false。在Objective-C 2.0中可能已經解決了這個問題,但是我總是爲了安全而做長手版本。
我忘了指定一件事,plist是這樣的(對不起): http://pastie.org/2238454 –
好的,仍然不太清楚你在問什麼。我現在知道你有一個plist,包含一個包含兩個更多字典的字典,每個字典中都有一個字符串/鍵,這兩個字符都是你想編輯的,是正確的嗎? –
然後保存plist? –