読者です 読者をやめる 読者になる 読者になる

知行合一

とか言ってみる

【Swift3】Alamofire4でのリクエストとレスポンス

//swift 3.0
let urlStr = "https://~~~"
Alamofire.request(urlStr, method: .get, parameters: nil, encoding: JSONEncoding.default, headers: nil).responseJSON { (response:DataResponse<Any>) in
    switch(response.result) {
    case .success(_):
        if let jsonResult = response.result.value as? [String: Any] {
            let ary = jsonResult["results"] as! NSArray
            //(もう一つ子までいきたいなら)
            //let ary = (jsonResult["results"] as! NSArray) ["artist"] as! NSArray
        }
        break
    case .failure(_):
        print(response.result.error)
        break
    }
}


//以前のバージョン
let urlStr = "https://~~~"
let req = request(.GET, method: urlStr)
req.response { (request, response, responseData, error) -> Void in
    do {
        let jsonResult = try NSJSONSerialization.JSONObjectWithData(responseData!, options: NSJSONReadingOptions.MutableContainers) as! NSMutableDictionary
        let ary = jsonResult["results"] as! NSArray
    } catch {
        print("catch")
    }
}