I am trying to get a string out from an external JSON file, which is on a web server, and it gets it successfully, but it is inside a closure where it gets the value, and I need to get it outside so I can return it with the variable returnip How do I do this?
func getJsonFromUrl() -> String {
let URL2 = "https://url.com/asd.php";
let url = URL(string: URL2)
URLSession.shared.dataTask(with:url!) { (data, response, error) in
if error != nil {
print(error as Any)
} else {
do {
let parsedData = try JSONSerialization.jsonObject(with: data!) as! [String:Any]
let ips = parsedData["ip"] as! String
print("The IP is: " + ips) //Prints the value correctly
var returnip = ips //The value that I want to return, that does not go outside this closure
} catch let error as NSError {
print(error)
}
}
}.resume()
return returnip //Does not return anything
}
Thanks