【问题标题】:save response in nsmutabledictionary ? swift 3.0在 nsmutabledictionary 中保存响应?迅捷3.0
【发布时间】:2016-12-01 10:48:06
【问题描述】:

我在 Alamofire 4.0 中获取响应的代码

Alamofire.request(webpath).responseJSON
    {
        response in

        //here I want to store response in nsmutabledictionary
    }

在该响应中,我有 Data 对象,其中包含如下数据

{"Success":"True","Data":[{"Id":"4","Name":"xyz"}]

我想将该 Data 对象保存在 NSMutableArray 中,以便在 UITableview 中显示

【问题讨论】:

  • 一如既往地不要在 Swift 中使用NSMutable...,除非你别无选择。给你。

标签: ios swift3 alamofire


【解决方案1】:

您可以像这样从Alamofire 响应中获得结果Dictionary

Alamofire.request(webpath).responseJSON
{
    response in

    switch(response.result) {
    case .success(_):
        if let dic = response.result.value as? [String: Any], 
           let array = dic["Data"] as? [[String: Any]] {
            print(array)
        }
    case .failure(_):
        print(response.result.error)
    }
}

注意:使用 Swift 原生 DictionaryArray 而不是 NSDictionaryNSArray

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-07
    相关资源
    最近更新 更多