swift - 如何让 Freddy 在 Swift 中使用 AlamoFire?

标签 swift alamofire

我正在尝试使用 Alamofire POST 请求对我的用户进行身份验证。我得到一个作为字典的响应对象。我希望我能得到一个Freddy该字典中的 LoginResponse 对象。 swift 对我来说是新的。

import UIKit
import Alamofire
import Freddy


public struct LoginResponse {
    public let Response: String
    public let Success: Int
    public let Time: String
}

extension LoginResponse: JSONDecodable {
    public init(json value: JSON) throws {
        Response = try value.string("Response")
        Success = try value.int("Success")
        Time = try value.string("Time")
    }
}


class LoginViewController: UIViewController {


    @IBOutlet weak var emailOutlet: UITextField!
    @IBOutlet weak var passwordOutlet: UITextField!
    @IBAction func LoginAction(sender: AnyObject){
        var parameters = [String: AnyObject]()
        parameters["email"] = self.emailOutlet.text
        parameters["password"] = self.passwordOutlet.text
        Alamofire.request(.POST, "https://cool.api/iot/users/login", parameters: parameters, encoding: .JSON)
            .responseJSON
            { response in switch response.result {
            case .Success(let JSON):
                print("Success with JSON: \(JSON)")

                let response = JSON as! NSDictionary
                print(response["Response"]!)


         // What I am trying !!
         //   do {
         //       let response = JSON as! NSDictionary
         //       // Assuming `json` has the JSON data
         //       let attrs = try JSON.array("SUCCESS")
         //       let theLoginResponseObject = try attrs.map(LoginResponse.init)    
         //   } catch {
         //     
         //   }


            case .Failure(let error):
                print("Request failed with error: \(error)")
                }
        }
    }


    override func viewDidLoad() {
        super.viewDidLoad()
        self.emailOutlet.text = "myemail@gmail.com"
        self.passwordOutlet.text = "passwd"
    }

}

控制台输出

Success with JSON: {
    Response = "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJkZXZpY2VzIjpbImM5NGYyZjY0MDkwZmU4MWFmZjk5MGNkNTU0OTZhZjhkIiwiZjdmOGYzNWI0NDRiYmM3NzcxNzYxNjhlNTcxZjgzNjUiXSwiZW1haWwiOiJmcmFuY2VzY29hZmVycmFyb0BnbWFpbC5jb20iLCJleHAiOjE0NjU3MzcyOTUsImlkIjoiNTc0Y2E0NDkzMTcyODUwMDAxNjkzOGQ2Iiwicm9sZSI6Im1hc3RlciJ9.P-QxGCUTi1YWq46HJQlR2K-4S_DBKFxOLiyzqvE-r7S96XSxx02dpT8jOlZm4gx2qVrcj5wFyowJzy8HtU-y030I6OmftGe_dn2AgMJCD8dLXrRiRWfnWK5nhN6BvDJqCLyN_BopKGM2stEf7stavoPogy4HxBfg_hWIFJEwdHs";
    Success = 200;
    Time = "2016-06-12T13:04:55.426208276Z";
}
eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJkZXZpY2VzIjpbImM5NGYyZjY0MDkwZmU4MWFmZjk5MGNkNTU0OTZhZjhkIiwiZjdmOGYzNWI0NDRiYmM3NzcxNzYxNjhlNTcxZjgzNjUiXSwiZW1haWwiOiJmcmFuY2VzY29hZmVycmFyb0BnbWFpbC5jb20iLCJleHAiOjE0NjU3MzcyOTUsImlkIjoiNTc0Y2E0NDkzMTcyODUwMDAxNjkzOGQ2Iiwicm9sZSI6Im1hc3RlciJ9.P-QxGCUTi1YWq46HJQlR2K-4S_DBKFxOLiyzqvE-r7S96XSxx02dpT8jOlZm4gx2qVrcj5wFyowJzy8HtU-y030I6OmftGe_dn2AgMJCD8dLXrRiRWfnWK5nhN6BvDJqCLyN_BopKGM2stEf7stavoPogy4HxBfg_hWIFJEwdHs

错误

/Users/cesco/code/iot/iot/LoginViewController.swift:50:37: Value of type 'NSDictionary' has no member 'array'

最佳答案

您可以使用以下 GET 调用来检索 JSON 数据以供 Freddy 使用。

Alamofire.request(.GET, "http://api.openweathermap.org/data/2.5/weather?APPID=<123456>").responseJSON { (response) -> Void in
      if response.result.isSuccess {
          do {
              let json = try JSON(data: response.data!)
              let description = try json.array("weather")[0]["description"]
              print(description)
          } catch {
              print("There is an error")
          }
      }
}

或者,如果您使用的是截至 2016 年 8 月最新的 AlamoFire:

Alamofire.request( "http://api.openweathermap.org/data/2.5/weather?APPID=<123456>", withMethod: .get).responseJSON { (response) -> Void in
      if response.result.isSuccess {
          do {
              let json = try JSON(data: response.data!)
              let description = try json.array("weather")[0]["description"]
              print(description)
          } catch {
              print("There is an error")
          }
      }
}

关于swift - 如何让 Freddy 在 Swift 中使用 AlamoFire?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37774461/

相关文章:

iOS 11 Swift 4 iPhone X Safe Area 支持全屏ScrollView

swift - 解析查询 findObjectsInBackgroundWithBlock 时出错

ios - 响应中缺少 Alamofire 参数

ios - Swift ios alamofire 数据在 viewDidLoad 中第一次返回空

ios - 错误域=NSPOSIXErrorDomain 代码=100 "Protocol error"

ios - 在 swift4 中保存 label.text

ios - 视觉框架提供不精确的滚动和偏航值

swift - 在 Collection View 中将相邻单元格向右移动

swift - 如何对来自 Alamofire 的 JSON 进行排序并返回最终的 JSON 对象 (swiftyJSON)

ios - Swift Alamofire 发送图像数组和其他参数