【问题标题】:Display Data from php inside Label Swift/AlamofirePHP using JSON使用 JSON 在 Label Swift/AlamofirePHP 中显示来自 php 的数据
【发布时间】:2022-06-22 17:19:48
【问题描述】:

我有点卡住,试图在 swift 中的标签内显示来自 php 的数据。

我使用php从数据库中检索数据,并成功打印在Xcode控制台中。

感谢你们的努力!

responseString = Optional({"name":"John","dateOfBirth":"10-10-1955"})

这是viewDidLoad里面的ViewController中使用的代码:

let request = NSMutableURLRequest(url: NSURL(string: "https://example.com/test.php")! as URL)

               request.httpMethod = "POST"

               let postString = "uid=\(userID)"

               request.httpBody = postString.data(using: String.Encoding.utf8)

               let task = URLSession.shared.dataTask(with: request as URLRequest) {

                   data, response, error in

                   if error != nil {

                       print("error=\(String(describing: error))")
                       return
                   }

                   print("response = \(String(describing: response))")

                   let responseString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)
                   print("responseString = \(String(describing: responseString))")

               }

               task.resume()

let Data = ServiceID()

        Data.downloadData { [self] Data in

                    if let currentInformation = Data {
                        print(currentInformation.name) // This doesn't execute
                        self.firstName.text = currentInformation.name

                    }
               }

这是我的 Model.swift 中获取 php 响应的代码


struct ServiceDataID {

    var name: String?

}

class ServiceID {

    func downloadData(completion: @escaping (ServiceDataID?) -> Void) {

        AF.request("https:/example.com/test.php”).responseString {

            response in
            print(response)
            if let json = response.result as? [String: Any] {

                guard let name = json["name"] as? Stringelse { completion(nil)
                    return
            }

           let data = ServiceDataID(name: name)

        }
    }

}
}


【问题讨论】:

    标签: php json swift alamofire


    【解决方案1】:

    添加

    let data = ServiceDataID(name: name)
    completion(data)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-20
      • 2016-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多