gpt4 book ai didi

swift - 快速访问协议(protocol)方法中的变量

转载 作者:行者123 更新时间:2023-11-30 13:38:54 26 4
gpt4 key购买 nike

任何人都可以帮助我使用这个快速代码,我是一个快速初学者。我正在尝试使用 MVC 创建一个登录页面,它看起来像这样 enter image description here

因此,当用户输入登录信息并且 JSON 数据的状态为“s”(表示成功登录)时,它应该显示一些测试信息,而当登录状态为“f”(表示登录失败)时,它应该保留在同一个登录页面,我尝试访问以下代码中协议(protocol)方法中的状态:

因此,每当用户按下按钮时,它都应该采取所需的操作,但下面代码中的状态为零,因此每当我运行应用程序时,无论登录是否成功,都会显示信息

//this piece of code is in the LoginViewController
func didReceivedLoginInfo(info : [LoginModel]) -> String
{
self.loginData = info
print("didReceivedLOOGIN count is \(loginData!.count)")//the print is successful here
for i in 0..<loginData!.count
{
print("LoginData Info : \(loginData![i])")//the print is successful here
}
statuss = loginData![0].status
print("HERE YOU GO, THIS IS WHAT I FOUND: \(statuss!)")//the print is successful here
return statuss!
}

//this piece of code is in the LoginViewController
func buttonAction(sender:UIButton!)
{
var btnsendtag: UIButton = sender
var uName = username!.text as? String!
var uPass = password!.text as? String!
parser = LoginParser()
parser!.getLoginInfo(uName!, pass: uPass!)
var status = loginData?[0].status
print("from View Controller this is the msg: \(status)")
parser!.delegate = self
if status == "s" {
print("from View Controller this is the status: \(status)")
let vc = ViewController()
let navigationController = UINavigationController(rootViewController: vc)
self.presentViewController(navigationController, animated: true, completion: nil)
}

else {
let vc = LoginViewController()
let navigationController = UINavigationController(rootViewController: vc)
self.presentViewController(navigationController, animated: true, completion: nil)
}
}



////this piece of code is in the LoginParser
import UIKit

@objc protocol LoginParserDelegate : NSObjectProtocol

{

func didReceivedLoginInfo (info : [LoginModel]) -> String

optional func didRecivedError()

}



class LoginParser: NSObject {

// var webData: NSMutableData?

var webData: NSData?
weak var delegate : LoginParserDelegate?
var name = String ()
var model : LoginModel?
var alertView:UIAlertView?
var MainLogin : [LoginModel] = []

func getLoginInfo (usern : String, pass : String){

var post:NSString = "LID=\(usern)&PWD=\(pass)&Passcode=s@v#"
var url:NSURL = NSURL(string: "some link")!
var postData:NSData = post.dataUsingEncoding(NSUTF8StringEncoding)!
var postLength:NSString = String( postData.length )

var request:NSMutableURLRequest = NSMutableURLRequest(URL: url)

request.HTTPMethod = "POST"

request.HTTPBody = postData

request.setValue(postLength as String, forHTTPHeaderField: "Content-Length")

request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")

request.setValue("application/json", forHTTPHeaderField: "Accept")

var response: NSURLResponse?

let connection = NSURLConnection(request: request, delegate: self)
}


func connection(connection: NSURLConnection, didFailWithError error: NSError){

if delegate != nil {
if delegate!.respondsToSelector(Selector("didRecivedLoginInfo")){
delegate!.didRecivedError!()
}

}
}

func connection(connection: NSURLConnection, didReceiveResponse response: NSURLResponse){

webData = NSData()


}

func connection(connection: NSURLConnection, didReceiveData data: NSData){

webData! = data
}

func connectionDidFinishLoading(connection: NSURLConnection){
let responseString = NSString(data: webData!, encoding: NSUTF8StringEncoding)
print("responseString \(responseString!)")

processUserTypes ()

}

func processUserTypes ()->[LoginModel]
{
model = LoginModel()

// Process JSON

do {
let result = try NSJSONSerialization.JSONObjectWithData(webData!, options: NSJSONReadingOptions.MutableContainers)as? NSDictionary


var Login = LoginModel()


let status = result?.objectForKey("Status") as? String
let msg = result?.objectForKey("En") as? String
let employeename=result?.objectForKey("DisplayName")as? String
print("STATUS IN PARSER IS \(status!)")

if status == "s"

{

print(status!)
Login.name = employeename
Login.status = status
Login.message = msg
alertView = UIAlertView()
alertView!.title = "Login successful"
alertView!.message = "Welcome \(employeename!)"
alertView!.delegate = self
alertView!.addButtonWithTitle("OK")
alertView!.show()
MainLogin.append(Login)
print("count in login is \(MainLogin.count)")
if delegate != nil
{
delegate!.didReceivedLoginInfo(MainLogin)
}

}

else

{
Login.message = msg
alertView = UIAlertView()
alertView!.title = "Login failed"
alertView!.message = msg!
alertView!.delegate = self
alertView!.addButtonWithTitle("OK")
alertView!.show()

MainLogin.append(Login)
print("this is the main login\(MainLogin)")


}

}
catch {

if delegate != nil {
if delegate!.respondsToSelector(Selector("didRecivedLoginInfo")){
delegate!.didRecivedError!()
}


}



}
return MainLogin
}
}

有人可以帮我解决这个问题吗?谢谢

最佳答案

您在processUserTypes内调用didReceivedLoginInfo,该过程在connectionDidFinishLoading内调用。 buttonAction 调用 getLoginInfo,后者执行异步请求,完成后将回调 connectionDidFinishLoading

getLoginInfo 将在发送请求、接收响应并调用 connectionDidFinishLoading 之前返回很长时间。

=> 调用 getLogininfo 后您将无法执行任何操作

=>

给你按钮方法

  1. 创建解析器
  2. 设置委托(delegate)
  3. 调用 getLoginInfo
  4. 不要在这里做任何有潜在结果的事情

用于 didReceiveLoginInfo。

你可以在那里做进一步的逻辑,甚至可能是用户界面的东西(你必须将影响用户界面的逻辑包装在

dispatch_async(dispatch_get_main_queue()){
//here
}

通话

关于swift - 快速访问协议(protocol)方法中的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35803455/

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