【问题标题】:Alamofire receive and parse an array of strings swiftAlamofire 快速接收并解析字符串数组
【发布时间】:2017-02-28 05:33:50
【问题描述】:

我得到这样的字符串数组的结果

["India","America","Australia","China","Russia"]

我正在使用 Alamofire 来使用代码获取响应。没有错误,但我得到的结果为空。请帮助解析这个。

sessionManager?.request(strURL, method: method, parameters: params, encoding: encoding , headers: headers).responseJSON { (response) in
   switch response.result {
        case .success:
            let resJson = JSON(response.result.value!)
           success(resJson)
            break
        case .failure(let error):
            failure(error as NSError)
            break
        }

    }

【问题讨论】:

  • 你在.plist中添加了transportsecurity
  • 是的....我正在使用允许任意加载
  • 然后像let resJson = response.result.value as [String]一样访问
  • 让 resJson = response.result.value as [String] 谢谢@Anbu.Karthik ....效果很好

标签: ios iphone swift swift3


【解决方案1】:

试试这个:

if let responseData = response.result.value{    
let responsevalue = responseData  as? [String]
}

【讨论】:

    【解决方案2】:

    对于任何寻找另一个派生答案的人,只需将这段代码放在Alamofire.request(...) 之后:

    .responseJSON(completionHandler: { (response) in
    
                switch response.result{
    
                case .success(let value):
    
                    // Here is your array of String
                    let arrayOfStrings = value as? [String]
    
                case .failure(let error):
    
                    // Some code when error happens...
                    print(error.localizedDescription)
    
                }
    
            })
    

    【讨论】:

      【解决方案3】:

      此解决方案使用 SwiftyJSON:

      .responseJSON(completionHandler: { (response) in
      
           switch response.result{
           case .failure(let error):
               print(error.localizedDescription)
      
           case .success(let res):
             let json = JSON(res)
             let res = json["result"]
      
             var models = [String]()
             if let models1 = company["models"].array {
                 for model in models1 {
                     guard let mod = model.string else { return }
                     models.append(mod)
                 }
             }
           }
      })
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-02-22
        • 1970-01-01
        • 1970-01-01
        • 2020-03-26
        • 2020-02-28
        • 1970-01-01
        • 2011-03-11
        • 1970-01-01
        相关资源
        最近更新 更多