gpt4 book ai didi

ios - Webservice 方法未获取第一次单击的值

转载 作者:行者123 更新时间:2023-11-30 12:29:26 25 4
gpt4 key购买 nike

在此我通过网络服务获得值(value)。它完美地获得了值(value)。为了测试,我使用了 print 语句,它显示了完美的值,但是当我将这些值分配给变量时,它不会在第二次单击时分配,我的变量正在获取值..第一次进入 else 时,但它应该进入 if 。

        let task = URLSession.shared.dataTask(with: url!) {(data, response, error) in
if error != nil
{
print("Error")
}

else
{
if let content = data
{
do
{
let myJson = try JSONSerialization.jsonObject(with: content, options: JSONSerialization.ReadingOptions.mutableContainers) as! NSDictionary




let asd = myJson.value(forKey: "signinResult")
print(asd!)
self.answer = String(describing: asd!)
var mystring = self.answer.components(separatedBy:"\"")

var mystrin = [String]()
let size = mystring.count
var i = 0
while (i < size-1 )
{
let st1 = mystring[i+1]

mystrin = st1.components(separatedBy:",")
self.em = mystrin[0]
print(self.em)
self.pw = mystrin[1]
print(self.pw)
self.na = mystrin[2]
print(self.na)
self.num = mystrin[3]
print (self.num)
i += 2

}


}

catch
{

}

}



}

task.resume()

if email.text == em && pwd.text == pw
{
GlobalVariable.myString=em
GlobalVariable.name=na
GlobalVariable.number=num
GlobalVariable.pwd = pw
GlobalVariable.i=1

self.performSegue(withIdentifier: "segue", sender: nil)


}
else
{
let alert = UIAlertController(title: "Alert", message: "Wrong Email and password", preferredStyle:UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler: nil))
self.present(alert,animated: true, completion: nil)
}

最佳答案

您在 Web 服务 block 中设置该变量的值,但在 block 外使用它,因此第一次当您单击发送时间时,它没有显示值,同时 Web 服务响应和值分配给该变量,因此获取它。

代码应该是这样的。

let task = URLSession.shared.dataTask(with: url!) {(data, response, error) in
if error != nil
{
print("Error")
}

else
{
if let content = data
{
do
{
let myJson = try JSONSerialization.jsonObject(with: content, options: JSONSerialization.ReadingOptions.mutableContainers) as! NSDictionary




let asd = myJson.value(forKey: "signinResult")
print(asd!)
self.answer = String(describing: asd!)
var mystring = self.answer.components(separatedBy:"\"")

var mystrin = [String]()
let size = mystring.count
var i = 0
while (i < size-1 )
{
let st1 = mystring[i+1]

mystrin = st1.components(separatedBy:",")
self.em = mystrin[0]
print(self.em)
self.pw = mystrin[1]
print(self.pw)
self.na = mystrin[2]
print(self.na)
self.num = mystrin[3]
print (self.num)
i += 2

}
if email.text == em && pwd.text == pw
{
GlobalVariable.myString=em
GlobalVariable.name=na
GlobalVariable.number=num
GlobalVariable.pwd = pw
GlobalVariable.i=1

self.performSegue(withIdentifier: "segue", sender: nil)


}
else
{
let alert = UIAlertController(title: "Alert", message: "Wrong Email and password", preferredStyle:UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler: nil))
self.present(alert,animated: true, completion: nil)
}

}

catch
{

}

}



}

task.resume()

关于ios - Webservice 方法未获取第一次单击的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43768106/

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