正如你可以從標題中看到的,我基本上想更新一個plist。實際上,應用程序用戶將在數據字段中鍵入他的數據,我會將它們保存在plist中。他可以添加儘可能多的物體,他喜歡。 我已經爲該員工嘗試了大量不同的代碼,並且他們都沒有工作。首先,來看看我的plist(我提出你的XML代碼,所以你可以把它理解)將數據添加到plist而不刪除以前的數據
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
<dict>
<key>name</key>
<string>TEST</string>
<key>code</key>
<string>TEST</string>
<key>longitude</key>
<integer>22</integer>
<key>latitude</key>
<integer>10</integer>
</dict>
</array>
</plist>
這僅僅是一個簡單的條目。現在這裏是我使用的lates代碼,但我沒有運氣
NSMutableDictionary *newBuilding = [NSMutableDictionary alloc];
[newBuilding setValue:@"Test1" forKey:@"name"];
[newBuilding setValue:@"Test2" forKey:@"code"];
[newBuilding setValue:@"10" forKey:@"latitude"];
[newBuilding setValue:@"20" forKey:@"longitude"];
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"BuildingsInfo.plist"];
NSMutableArray *newArray = [NSMutableArray arrayWithContentsOfFile:plistPath];
if (nil == newArray) {
newArray = [[NSMutableArray alloc] initWithCapacity:0];
}
[newArray addObject:newBuilding];
[newArray writeToFile:plistPath atomically: YES];
}
任何人都可以發現任何錯誤嗎?我已經寫了很多次,我已經檢查了每個標記,但是我沒有發現任何錯誤。我不明白爲什麼我不想工作!
非常感謝球員
我剛剛讀過plist必須在文檔目錄中,以便我能夠在上面寫下它。一旦程序被加載,任何人都可以讓我知道如何將plist傳輸到文檔目錄中? – user1015777 2012-03-03 00:17:26