swift - Alamofire 协议(protocol)返回模型对象数组

标签 swift alamofire

我正在使用 alamofire,它是序列化协议(protocol)。我有一个模型,效果很好。现在,我到底如何从这些方法中获取该模型的数组?

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

    }

最佳答案

您是否从请求中获得了 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
        }
    }

关于swift - Alamofire 协议(protocol)返回模型对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37355711/

相关文章:

ios - Swift 跨应用程序身份验证不起作用

swift - 使用自动布局隐藏/显示图像?

ios - Firebase动态链接不会缩短URL

ios - 在 Xcode 中编译应用程序时无法解决的链接器错误

json - API 可以不理解我的属性类型吗?

ios - 如何使用 alamofire 实现 put http 请求

ios - swift 错误 : "MailCompositionService quit unexpectedly"

ios - Alamofire 监控网络状态

swift - 使用 Swift 通过 REST API 为 youtube 上传设置片段数据

swift - 从 'Result<AnyObject>' 转换为不相关类型 'NSArray' 总是失败