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)")


            }
        }

    } 

1 Answer 1

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>{
        }
    }

}
Sign up to request clarification or add additional context in comments.

6 Comments

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<String, AnyObject>
In that case I suggest you use NSMutableDictionary instead of using swift Dictionary.
@JeffA I changed my answer to use NSMutableDictionary instead of swift Dictionary. Have a look.
Unfortunately this is not working with the current Alamofire request as it is expecting a Dictionary<String, AnyObject>. 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 post to do so.
@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.
|

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.