Swift - Delegate Protocol structure

2019-03-03 01:22发布

问题:

Forgive me for being fairly new to swift. I wanted to create a delegate that used to "POST" request and get response from server. Then pass the parameters returned from server into func and allows my main class to response accordingly. Below is my delegate and my main class. It did not print out response log in LoginViewController class. I'm kinda lost and not much idea where it went wrong. Suggestions / pointers on how can I get this to work or better way on doing this is highly appreciated. Thanks!

Delegate class

import Alamofire

protocol ServletDelegate {

   func responseSuccess(sender: Servlet, respArray : [String])

   func responseFail(sender: Servlet, respArray : [String])
} 

class Servlet : UIViewController{

    var delegate:ServletDelegate?

    //form 'data' parameter


    Alamofire.request(.POST, Config.flURL, parameters:["data" : ""]).responseData {
      response in
        print ("request \(response.request)")
        print ("response \(response.response)")
        print ("result \(response.result)")
        print ("data \(response.data)")

        let responseData = NSString (data: response.data!, encoding: NSUTF8StringEncoding)
        result = (responseData?.componentsSeparatedByString("~z0@"))!

        print("responseData :\(responseData)");
        print("result :\(result)");

        self.delegate?.responseSuccess(self, respArray: result);

}

Main Class

class LoginViewController: Servlet, UITextFieldDelegate, ServletDelegate {

    @IBAction func submitBtnAction(sender: UIButton) {

        Servlet().LoginUserHttp(tfUsername.text!, userPassword: tfPassword.text!);

    }

    func responseSuccess(sender: Servlet, respArray: [String]) {
        print("responseSuccess: \(respArray)");
    }

    func responseFail(sender: Servlet, respArray: [String]) {
        print("responseFail: \(respArray)");
    }

}

回答1:

First of all your servlet class shouldnt be a UIViewController, second - don't use ; in swift its redundant, third you didn't set the delegate, fourth yeah delegate should be weak so that you dont run into strong ref cycle, fifth i don't see a reason to pass the sender

By the way you probably should pass some error object with the response failed delegate, but thats up to you

Delegate class

import Alamofire

protocol ServletDelegate: class {

   func responseSuccess(respArray : [String])

   func responseFail(respArray : [String])
} 

class Servlet {

    weak var delegate:ServletDelegate?

    //form 'data' parameter


    Alamofire.request(.POST, Config.flURL, parameters:["data" : ""]).responseData {
      response in
        print ("request \(response.request)")
        print ("response \(response.response)")
        print ("result \(response.result)")
        print ("data \(response.data)")

        let responseData = NSString (data: response.data!, encoding: NSUTF8StringEncoding)
        result = (responseData?.componentsSeparatedByString("~z0@"))!

        print("responseData :\(responseData)")
        print("result :\(result)")

        self.delegate?.responseSuccess(respArray: result)

}

Main Class

class LoginViewController: UIViewController, UITextFieldDelegate, ServletDelegate {


let servlet = Servlet()

@IBAction func submitBtnAction(sender: UIButton) {

/// im guessing you know what you are doing here, because we dont see that LoginUserHttp in your Servlet code        
servlet.LoginUserHttp(tfUsername.text!, userPassword: tfPassword.text!)

    }
override func viewDidLoad() {
    servlet.delegate = self
}

func responseSuccess(respArray: [String]) {
        print("responseSuccess: \(respArray)")
    }

    func responseFail(respArray: [String]) {
        print("responseFail: \(respArray)")
    }

}