3

RESOLVED! See the solution at the bottom of this post

I'm trying to create a JSON object to use for my backend using Alamofire. I am able to add a key with a String value but can't seem to be able to add a value of Array to AnyObject. I though it would be very straight forward but I haven't been able to find a solution.

func someFunction(btn: UIButton){

        var someDictionary = Dictionary<String, AnyObject>()
        let someArray = [textField[1].text,textField[2].text,textField[3].text]
        someDictionary["answer"] = textField[0].text
        someDictionary["options"] = someArray as? Array // <---- Can't be assigned to AnyObject

        let url = "http://localhost:3000/api/question"
        Alamofire.request(.POST, url, parameters: someDictionary).responseJSON { response in
            if let JSON = response.result.value as? Dictionary<String, AnyObject>{   
            }
        }

 }

Solution: Removed as? Array and created loop to append initialized Array

func someFunction(btn: UIButton){



        var someDictionary = Dictionary<String, AnyObject>()
        var SomeArray = [String]()
        for i in 1...3{                          //created loop to append the textField text
            SomeArray.append(textField[i].text!)
        }
        someDictionary["answer"] = textField[0].text
        someDictionary["options"] = SomeArray // Removed "as? Array"
        let url = "http://localhost:3000/api/question"
        Alamofire.request(.POST, url, parameters: someDictionary).responseJSON { response in
            if let JSON = response.result.value as? Dictionary<String, AnyObject>{
                print("JSON Response From Server-->\(JSON)")


            }
        }

    } 
Jeff A
  • 72
  • 6

1 Answers1

2

Clean your project and run again. Your code is working for me and I can assign

func someFunction(btn: UIButton){

    var someDictionary = Dictionary<String, AnyObject>()
    let someArray = ["SomeString","SomeString","SomeString"]
    someDictionary["answer"] = textFields[0].text
    someDictionary["options"] = someArray // now you can assign

    let url = "http://localhost:3000/api/question"
    Alamofire.request(.POST, url, parameters: someDictionary).responseJSON { response in
        if let JSON = response.result.value as? Dictionary<String, AnyObject>{
        }
    }

}
user3815344
  • 243
  • 1
  • 21
  • Thanks for the response. This solves the problem with the array but I also need to pass a String value into the Dictionary at the `someDictionary["answer"] = textFields[0].text` line. Is there a way to allow both Strings and Arrays into the Dictionary? EDIT: After testing your solution It seems the Alamofire request need the type Dictionary – Jeff A Apr 23 '16 at 04:08
  • 1
    In that case I suggest you use NSMutableDictionary instead of using swift Dictionary. – user3815344 Apr 23 '16 at 04:10
  • 1
    @JeffA I changed my answer to use NSMutableDictionary instead of swift Dictionary. Have a look. – user3815344 Apr 23 '16 at 04:15
  • Unfortunately this is not working with the current Alamofire request as it is expecting a Dictionary. It looks like I will need to create a custom Alamofire request to use NSMutableDictionary. I'm going to try and refactor the solution in [this](http://stackoverflow.com/questions/27026916/sending-json-array-via-alamofire) post to do so. – Jeff A Apr 23 '16 at 04:41
  • 1
    @JeffA just remove the as?. everything works fine. Do a good clean and run your code. No need of using NSMutableDictionary or customizing your frameworks. – user3815344 Apr 23 '16 at 04:59
  • Thanks! That resolved my issue. Also I had the "SomeString" being passed as textField.text. I needed to pass them into a loop and append them to the array to get it work properly. I will edit the solution to my post – Jeff A Apr 23 '16 at 06:02