Alamofire POST方法中的POST对象数组 - Swift / IOS

时间:2016-03-03 13:58:57

标签: ios swift alamofire

我试图使用Alamofire发布多个对象的数组,如下所示:

class Person {

dynamic var firstName: String?
dynamic var lastName: String?
dynamic var email: String?
dynamic var state: String?

init(){}
init(firstName:String, lastName:String, email:String){
    self.firstName = firstName
    self.lastName = lastName
    self.email = email
} }

我创建了一个人员数组并将其添加到字典中,如下所示:

let zipCode = “20876”
    let city = “Rockville”
    let persons = [Person]()
    let state = “MD”

    let params: [String:AnyObject] = [
        “zipCode” : zipCode!,
        “city” : city!,
        “persons”: NSArray(array: persons),
        “state” : state!

    ]

然后尝试将其发送到Web服务,我收到一条错误消息:由于未捕获的异常终止应用程序&#39; NSInvalidArgumentException&#39;,原因:&#39; JSON写入中的无效类型'< / p>

Alamofire.request(.POST, url, parameters: params, encoding: .JSON)
        .responseJSON {(req, res, json, error) in }

有没有人试过发布一个数组,alamofire是否能够这样做? 我希望我提出问题。对不起,如果这个问题太简单无法回答,我花了一整天的时间来解决这个问题,但没有成功。 谢谢

2 个答案:

答案 0 :(得分:0)

你必须从人类对象创建字体,该对象不可服用

class Person {

    dynamic var firstName: String?
    dynamic var lastName: String?
    dynamic var email: String?
    dynamic var state: String?

    init(){}
    init(firstName:String, lastName:String, email:String){
        self.firstName = firstName
        self.lastName = lastName
        self.email = email
    }
    func getDictFormat() -> [String: String]{

        return ["firstName" : firstName!, "lastName" : lastName!, "email" : email!, "state" : state!]
    }
}

答案 1 :(得分:0)

Ray Wenderlich最近有关于Alamofire的教程。

请参阅链接:

http://www.raywenderlich.com/85080/beginning-alamofire-tutorial  (请参阅创建请求路由器) http://www.raywenderlich.com/87595/intermediate-alamofire-tutorial