在Xcode中使用干净的Swift架构的注册页面出现问题

时间:2019-02-04 07:40:03

标签: swift clean-architecture

我目前正在我正在处理的聊天应用程序中实现注册功能。在我的“ SignupViewController”中,我想实现一个名为“ signupButtonPressed”的函数,该函数将我从注册视图控制器路由到“ ListContacts”视图控制器。如果注册失败,则将执行一个名为“ showValidationError”的函数。下面是我的SignupViewController的代码摘录:

================Sending  Sample delay command==============
[28][03][00][A4][00][01][C2][10]
Waiting for a confirmation...
<28><03><02><01><2C><E5><CF>

Sample delay result:1

 Sample delay : 
 : 2c    : 00    : 00    : 00    : 00    : 00    : 00    : 00    : 00    : 00    : 00    : 00    : 00    : 00    : 00    : 00    : 00    : 00    : 00    : 00    : 08    : 08    : 80    : 00    : 38    : 00    : 29    : 00    : 00    : 00    : c8    : 7d    : 01    : 00    : 00    : 00    : 30    : 00    : 00    : 00   


================Sending  measurement command==============
[28][10][00][01][00][05][0A][B1][D8][77][1E][B1][D8][77][1E][0D][F8][9F][E7]
Waiting for a confirmation...
<28><10><00><01><00><05><56><33>

Measurement result : 5 
: d8    : 1e    : d8    : 1e    : f8    : 00    : 65    : 57    : 70    : 00    : 10    : 02    : 65    : 57    : 7c    : 00    : 00 : 04   : 01    : 00    : 49    : 01    : 31    : 00    : 00    : 00    : 00    : 00    : 00    : 00    : 00    : 00    : 00    : 00 : 00   : 00    : 00    : 00    : 00    : 00    
================Sending  actual measurement command==============
[28][03][00][53][00][0A][32][25]
Waiting for a confirmation...
<28><03><14><41><DD><8E><59><00><00><00><00><00><00><00><00><46><1C><34><00><46><1C><34><00><48><84>

Read Return actual_measurement 3:10


 : dd    : 59    : 00    : 00    : 00    : 00    : 1c    : 00    : 1c    : 00    : 00    : 00    : 00    : 00    : 00    : 00    : 00    : 00    : 00    : 00    : 08    : 1b    : 31    : 00    : 28    : 00    : 08    : 00    : 01    : 00    : 00    : 00    : 00    : 00    : 20    : 07    : 00    : 00    : 20    : 07   
�AY�

我正在使用Swift Clean Architecture,因此,我还将代码链接到我的Signup Router,模型和Interactor文件:

1)Signupinteractor.swift:

@IBAction func signupButtonPressed(_ sender: Any) {
    let request = Signup.Request(
    name: fullNameTextField.text!,
    email: emailTextField.text!,
    password: passwordTextField.text!
    )

    interactor?.createAccount(request: request)   
}

func showValidationError(_ message: String) {
    let alertCtrl = UIAlertController(title: "Oops! An error occurred", message: message, preferredStyle: .alert)
    alertCtrl.addAction(UIAlertAction(title: "Ok", style: .cancel, handler: nil))
    self.show(alertCtrl, sender: self)
}

2)SignupModels.swift:

import Foundation

protocol SignupBusinessLogic {
    func createAccount(request: Signup.Request)
}

class SignupInteractor: SignupBusinessLogic {
    var viewController: SignupFormErrorLogic?
    var router: (NSObjectProtocol & SignupRoutingLogic)?
    var worker = UsersWorker()

    func createAccount(request: Signup.Request) -> Void {
        self.worker.signup(request: request) { user, error in

            guard error == nil else {

               print(error!)
                self.viewController?.showValidationError("Error creating account!")
                return
            }

            self.router?.routeToListContacts()
        }
    }
}

3)SignupRouter.swift:

import Foundation

enum Signup {
    struct Request {
        var name: String
        var email: String
        var password: String
    }

    struct Response {
        var user: User?

        init(data: [String:Any]) {
            self.user = User(
                id: data["id"] as! Int,
                name: data["name"] as! String,
                email: data["email"] as! String,
                chatkit_id: data["chatkit_id"] as! String
            )
        }
    }
}

我的UsersWorker.swift文件中的注册功能:

import UIKit

@objc protocol SignupRoutingLogic {
    func routeToListContacts()
}

class SignupRouter: NSObject, SignupRoutingLogic {
    weak var viewController: SignupViewController?

    func routeToListContacts() {
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let destinationVC = storyboard.instantiateViewController(withIdentifier: "MainNavigator") as! UINavigationController

        viewController!.show(destinationVC, sender: nil)
    }
}

当我在注册UITextfields(fullNameTextField; emailTextField; passwordTextField)中输入注册详细信息并按下注册按钮时,会触发一个名为“ CannotSignup”的错误。但是不确定为什么。这种情况也可以在我的UsersWorker.swift文件中找到:

func signup(request: Signup.Request, completionHandler: @escaping (User?, UsersStoreError?) -> Void) {
    let params: Parameters = [
        "name": request.name,
        "email": request.email,
        "password": request.password
    ]

    postRequest("/api/users/signup", params: params, headers: nil) { data in
        guard data != nil else {
            return completionHandler(nil, UsersStoreError.CannotSignup)
        }

        let response = Signup.Response(data: data!)
        CurrentUserIDDataStore().setID(CurrentUserID(id: response.user?.chatkit_id))

        let request = Login.Account.Request(
            email: request.email,
            password: request.password
        )

        self.login(request: request) { token, error in
            guard error == nil else {
                return completionHandler(nil, UsersStoreError.CannotLogin)
            }

            DispatchQueue.main.async {
                completionHandler(response.user, nil)
            }
        }
    }
}

如果有人能够查看代码以了解问题的根源以及如何解决这个问题,那将是很好的选择?如果需要进一步的信息,请询问!

1 个答案:

答案 0 :(得分:0)

很可能是API调用失败,请检查返回的HTTP状态代码而不是window.addEventListener('hashchange', (event: HashChangeEvent) => { const { target: { location: { hash } } } = event; /* ... */ }); 。在某些情况下,没有任何响应数据,API调用即可成功 理想情况下,还要发送一个TS2339: Property 'location' does not exist on type 'EventTarget | null'. 实例以及从data方法返回的数据