如何在swift4中使用Rxalamofire与网络通信

时间:2019-04-04 07:46:46

标签: ios swift4

我是RxSwiftRxAlamofire的新手,现在我的任务是发送Rxalamofire.I的URL请求,完全不知道Rxalamofire。以前,我只使用alamofire。我尝试使用RxSwiftRxalamofire发送url请求,但是由于出现诸如“使用未声明的类型'按钮结果第一行的“ EmptyResult”。我在下面提供了我尝试过的代码。因此,有人可以告诉我这样做吗。

import UIKit
import Alamofire
import RxAlamofire
import RxSwift
import RxCocoa


enum PostFriendFailureReason: Int, Error {
    case unAuthorized = 401
    case notFound = 404
}
class ViewController: UIViewController {


    @IBOutlet weak var Username: UITextField!
    @IBOutlet weak var Password: UITextField!
    override func viewDidLoad() {
        super.viewDidLoad()


    }

    @IBAction func register(_ sender: Any) {

        typealias PostFriendResult = EmptyResult<PostFriendFailureReason>

        typealias PostFriendCompletion = (_ result: PostFriendResult) -> Void

        func postFriend(username: String, password: String, completion: @escaping PostFriendCompletion) {
            let param = ["username": username,
                         "password": password]
            Alamofire.request("http://prydodriver-env.s4y4ssms6v.ap-south-1.elasticbeanstalk.com/prydo/driver/v1/user/login/", method: .post, parameters: param, encoding: JSONEncoding.default)
                .validate()
                .responseJSON { response in
                    switch response.result {
                    case .success:
                        completion(.success)
                    case .failure(_):
                        if let statusCode = response.response?.statusCode,
                            let reason = PostFriendFailureReason(rawValue: statusCode) {
                            completion(.failure(reason))
                        }
                        completion(.failure(nil))
                    }
            }
        }
    }
}

0 个答案:

没有答案