Search code examples
iosalamofire

array of objects set to be parameter when using POST request of Alamofire


I am using Alamofire to POST a request to the server, the body of the request contains one parameter that in the form of :

list: [ { "phone":"13917115283", "name": "Sky" }, { "phone":"13689652145", "name": "RJ" } ]

Any idea how to post such a request? Thanks


Solution

  • You can easily achieve this easily by using SwiftyJSON

    Install it via Cocoapods or manually. For manually you can simply download and add the SwiftyJSON.xcodeproj file in your workspace.

    Add SwiftyJSON framework for iOS in Build Phases

    enter image description here

    Now simply import it in your ViewController

     import SwiftyJSON
    

    Even after importing if Xcode doesn't recognize it.Clean and build it.

    Code

    var arrDic: NSArray = [
          ["phone": "13917115283", "name": "Sky"]
        , ["phone": "13689652145", "name": "RJ"]
                            ]
    let response = [ "list" : JSON(arrDic) ]
    
    print(JSON(response))
    

    Final Output

    enter image description here