jeudi 13 septembre 2018

Swift Xcode don't change var inside of "do" block

return ig is still -> "" any Ideas why?

import UIKit

class GetData: NSObject {
    func getData(jsonUrl: String, dataBase: String, value: String) throws -> String{
        // here we are initializing the URL Struct with the path which we want to access from the API
        let url = URL(string: jsonUrl)!

        let urlSession = URLSession.shared
        let getRequest = URLRequest(url: url)
        var ig = ""

        let task = urlSession.dataTask(with: getRequest as URLRequest, completionHandler: { data, response, error in

            guard error == nil else {
                return
            }

            guard let data = data else {
                return
            }

            print(data)

            do {
                let json = try JSONSerialization.jsonObject(with: data, options: [])

                //erstelle array aus json ergebnis
                guard let jsonArray = json as? [[String: Any]] else {
                    return
                }

                if(dataBase == "User"){
                //einzelndes jsonobjekt

                    if(value == "pw"){
                        guard let password = jsonArray[0]["password"] as? String else { return }
                        ig = password
                    }else if(value == "em"){
                        guard let email = jsonArray[0]["email"] as? String else { return }
                        ig = email
                    }


                    /*
                     guard let id = jsonArray[0]["id"] as? String else { return }
                     guard let username = jsonArray[0]["username"] as? String else { return }
                     guard let birthday = jsonArray[0]["birthday"] as? String else { return }
                     print(username)
                    */
                }else {
                    print("No Database selected")
                }
            } catch {
                print(error)
            }

        })
        task.resume()
        return ig



    }
}

Aucun commentaire:

Enregistrer un commentaire