我有一個plist文件(「myAwayList.plist」)來存儲導航數據。我已經在主字典嵌套字符串變量的字典中的數組如下..如何使用Swift在plist文件中添加新陣列數據到NSDictionary
<dict>
<key>Locations</key>
<array>
<dict>
<key>Name</key>
<string>Sydney City Centre</string>
<key>Address</key>
<string>Centrepoint Tower</string>
<key>Lat</key>
<string>-33.870451</string>
<key>Lon</key>
<string>151.208771</string>
</dict> ...
我可以成功地加載並用以下使用內部的陣列數據...
let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true) as NSArray
let documentsDirectory = paths.objectAtIndex(0)as NSString
let path = documentsDirectory.stringByAppendingPathComponent("myAwayList.plist")
let fileManager = NSFileManager.defaultManager()
// Check if file exists
if(!fileManager.fileExistsAtPath(path))
{
// If it doesn't, copy it from the default file in the Resources folder
let bundle = NSBundle.mainBundle().pathForResource("myAwayList", ofType: "plist")
fileManager.copyItemAtPath(bundle!, toPath: path, error:nil)
println("File did not exist! Default copied...")
}
let dict = NSDictionary(contentsOfFile: path)!
let mySavedLocations: AnyObject = dict.objectForKey("Locations")!
println("plist all: \(mySavedLocations)")
if let nsArray:NSArray = mySavedLocations as? NSArray{
for var loadCount = 0; loadCount < mySavedLocations.count; ++loadCount {
var locationDict:AnyObject = nsArray[loadCount] // loading array data at index
let arrayName = locationDict["Name"] as AnyObject? as String
let arrayAddress = locationDict["Address"] as AnyObject? as String
let arrayLat = locationDict["Lat"] as AnyObject? as String
let arrayLon = locationDict["Lon"] as AnyObject? as String
awaydatalist.append(AwayData(name:arrayName, address:arrayAddress, lat:arrayLat, lon:arrayLon)) // This is a list array used to display the loaded data on a ViewController
}
}
我現在想要將數組添加到另一行數據中,然後寫回plist文件,但我找不到一種方法來完成它。請指教? 克里斯
你想在數組中添加新的字典並更新plist.Right中的數據? – 2015-01-21 08:35:08