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)");
}
}
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
Main Class