0

I am teaching myself Swift these days and have trouble understanding how to update values in dictionaries stored in an array. The array consists of dictionaries read from file using JSON (I am using SwiftyJSON). The JSON file looks like this:

 {
  "test2" : [
    {
      "files" : "203",
      "url" : "Optional(\"\/Users\/heisan\/test\/\")",
      "date" : "April 6, 2015 at 15:23:40 EDT"
    }
  ]
}

Each key:value pair in the dictionaries consists of String:Array where the Array again consists of String:String dictionaries. My function for updating the content for specific key looks like this:

 @objc func addOptionForKey(key: String, date: NSDate, url: NSURL, files: NSNumber)-> Bool {

        let dictData: NSData? = self.createDataOfDictionary()
        if (dictData != nil) {
            let json = JSON(data: dictData!)

            for (localkey: String, var subJson: JSON) in json {

                var backups: Array = [Dictionary<String,AnyObject>]()

                var restores = subJson.arrayValue
                for restore in restores {
                    let innerDict: [String:String] = ["url":restore["url"].stringValue,
                        "files":restore["files"].stringValue,
                        "date":restore["date"].stringValue]

                    backups.append(innerDict)
                }
                self.restoreDict[localkey]=backups    
        } 
        return true
}

Here, a the restoreDict property is defined as

self.restoreDict = String:[AnyObject]

The function crashes when I try to update my array for the specific key:

self.restoreDict[localkey]=backups

I am sure I am doing some amateur error here but a kick in the right direction would be greatly appreciated. Thanks. T

Update: My problem turned out to be related to my version of Xcode and Swift. When I switched to Xcode 6.3 + Swift 1.2 as well as moving away from using swiftJSON (as Swift 1.2 has many improvements for handling JSON) everything works as expected.

1 Answer 1

1
var restoreDict = [String:[String:AnyObject]]()

let test2 = ["files" : 203, "url" : "blah", "date" : "April 6, 2015 at 15:23:40 EDT"]

restoreDict["local"] = test2

println(restoreDict)

// prints "[local: [files: 203, url: blah, date: April 6, 2015 at 15:23:40 EDT]]"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.