WebService方法未获得第一次单击的值



在此我通过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
                                }

                    }
                    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服务块中设置该变量的值,但在块外使用它,因此首次单击时,它没有值得显示的值对于这个变量,因此得到它。

代码应该是这样的。

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

相关内容

  • 没有找到相关文章

最新更新