gpt4 book ai didi

Swift - 委托(delegate)协议(protocol)结构

转载 作者:搜寻专家 更新时间:2023-11-01 06:41:16 24 4
gpt4 key购买 nike

请原谅我对 swift 还很陌生。我想创建一个用于“POST”请求并从服务器获取响应的委托(delegate)。然后将从服务器返回的参数传递给 func 并允许我的主类相应地响应。下面是我的委托(delegate)和我的主要类(class)。它没有打印出 LoginViewController 类中的响应日志。我有点迷路,不知道哪里出了问题。非常感谢有关如何使它工作或更好的方法的建议/指示。谢谢!

委托(delegate)类

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);

}

主类

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

}

最佳答案

首先,您的 servlet 类不应该是 UIViewController,其次 - 不要使用 ;很快它是多余的,第三你没有设置委托(delegate),第四是委托(delegate)应该是弱的,这样你就不会遇到强引用循环,第五我没有看到通过发送者的理由

顺便说一句,你可能应该传递一些带有响应失败委托(delegate)的错误对象,但这取决于你

委托(delegate)类

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)

}

主类

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)")
}

}

关于Swift - 委托(delegate)协议(protocol)结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34938682/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com