Alamofire协议返回模型对象的数组

时间:2016-05-20 20:55:19

标签: swift alamofire

我使用的是alamofire及其序列化协议。我有一个模型,它的工作很棒。现在,我究竟如何从这些方法中获得该模型的数组?

static func collection(response response: NSHTTPURLResponse, representation: AnyObject) -> [DataObject] {

        var daos: [DataObject] = []
        if let representation = representation as? [[String: AnyObject]] {
            for contentRepresentation in representation {
                if let content = DataObject(response: response, representation: contentRepresentation) {
                    daos.append(content)
                }
            }
        }
        return daos
    }

class func populateData() {
        Alamofire.request(.GET, url)
            .responseCollection { (response: Response<[DataObject], NSError>) in

         //response.result.value how do i pass this to my viewcontroller?
        }

    }

1 个答案:

答案 0 :(得分:0)

您是否从请求中获得了JSON响应?

如果是这样,您可以尝试以下方式:

Alamofire.request(.GET, url).responseJSON {
        response -> () in

        switch response.result {
        case .Success(let object):
            if let urlResponse = response.response {
                let dataObjects = collection(response: urlResponse, representation: object)
                // Pass the objects back to your view controller here
            }
            break

        case .Failure(let error):
            print("Error: ", error)
            break
        }
    }