2015-01-21 40 views
0

我有一個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文件,但我找不到一種方法來完成它。請指教? 克里斯

+0

你想在數組中添加新的字典並更新plist.Right中的數據? – 2015-01-21 08:35:08

回答

0

下面是添加新的詞典存儲在plist文件

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 = NSMutableDictionary(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 
     } 
    } 

    //Convert AnyObject to NSMutableArray 
    var twDataArray = (mySavedLocations as NSMutableArray) as NSMutableArray 

    //Create a new Dictionary 
    let airports: [String: String] = ["Name": "Toronto Pearson", "Address": "Dublin","Lat":"23.23","Lon":"78.89"] 

    //add dictionary to array 
    twDataArray.addObject(airports) 

    //set the new array for location key 
    dict.setObject(mySavedLocations, forKey: "Locations") 

    //update the plist 
    dict.writeToFile(path, atomically: true) 

此代碼添加位置的陣列內的新詞典陣列內的代碼。

+0

謝謝,我可以按照你的代碼,但它在線崩潰... twDataArray.addObject(airports);錯誤「libC++ abi.dylib:終止於類型爲NSException的未捕獲異常」??? – 2015-01-21 10:10:32

+0

......即使沒有分號「;」 ...仍然崩潰;-) – 2015-01-21 10:25:45

+0

我將此代碼添加到viewDidLoad()方法和它的工作完美。 – 2015-01-21 11:50:56

相關問題