Swift - Why doesn't this find the variable (let)? [duplicate] - ios

This question already has answers here:
Swift closure async order of execution
(1 answer)
Returning data from async call in Swift function
(13 answers)
Returning method object from inside block
(3 answers)
Closed 5 years ago.
func isEmailTaken(email:String) -> String {
let myUrl = URL(string: "URL");
var request = URLRequest(url:myUrl!)
request.httpMethod = "POST"
let postString = "email=\(email)";
request.httpBody = postString.data(using: String.Encoding.utf8);
let task = URLSession.shared.dataTask(with: request) { (data: Data?, response: URLResponse?, error: Error?) in
if error != nil {
print("error=\(error)")
return
}
print("response = \(response)")
do {
let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary
if let parseJSON = json {
let emailAlreadyTakenData = parseJSON["emailAlreadyTaken"] as! String
print(emailAlreadyTakenData)
}
} catch {
print(error)
}
}
task.resume()
return(emailAlreadyTakenData)
}
The line :
return(emailAlreadyTakenData)
Doesnt get the variable value. So, the http request gets the data successfully but the return command doesn't parse the correct data.

Your variable is in different scope, then when you declared it. So you cannot access the variable outside of the defined scope

Related

iOS - Swift : fetching data from database in main thread, not in background

In my iOS App i'm able to download data from a database, but actually all the operations are made in background and the main thread is still active, even the GUI. I also tried to make a 'sleep' with
DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(3)) { ... }
With this delay everthing works fine, but it's not a good solution. How can i change my code to do this in the main thread? Possibly with loadingIndicator.
This is my code (checking if username exists):
func CheckIfUsernameExists(username : String, passwordFromDb : inout String, errorMsg : inout String)
{
//declare parameter as a dictionary which contains string as key and value combination. considering inputs are valid
var _errorMsg = ""
var _psw = ""
var parameters : [String : Any]?
parameters = ["username": username,
"action": "login"]
print(parameters!)
let session = URLSession.shared
let url = "http://www.thetestiosapp.com/LoginFunctions.php"
let request = NSMutableURLRequest()
request.url = URL(string: url)!
request.httpMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.setValue("application/json", forHTTPHeaderField:"Accept")
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField:"Content-Type")
do{
request.httpBody = try JSONSerialization.data(withJSONObject: parameters!, options: .sortedKeys)
let task = session.dataTask(with: request as URLRequest, completionHandler: {(data, response, error) in
if let response = response {
let nsHTTPResponse = response as! HTTPURLResponse
let statusCode = nsHTTPResponse.statusCode
print ("status code = \(statusCode)")
}
if let error = error {
print ("\(error)")
}
if let data = data {
do{
_psw = self.parseJSON_CheckIfUsernameExists(data, errorMsg: &_errorMsg)
}
}
})
task.resume()
}catch _ {
print ("Oops something happened buddy")
errorMsg = "Usarname non recuperato (1)"
}
passwordFromDb = _psw
errorMsg = _errorMsg
}
You’re attempting to update passwordFromDb and errorMsg at the end of this method. But this is an asynchronous method and and those local variables _psw and _errorMsg are set inside the closure. Rather than trying to defer the checking of those variables some arbitrary three seconds in the future, move whatever “post request” processing you need inside that closure. E.g.
func CheckIfUsernameExists(username : String, passwordFromDb : inout String, errorMsg : inout String) {
//declare parameter as a dictionary which contains string as key and value combination. considering inputs are valid
let parameters = ...
let session = URLSession.shared
var request = URLRequest()
...
do {
request.httpBody = ...
let task = session.dataTask(with: request) { data, response, error in
if let httpResponse = response as? HTTPURLResponse,
let statusCode = httpResponse.statusCode {
print ("status code = \(statusCode)")
}
guard let data = data else {
print (error ?? "Unknown error")
return
}
let password = self.parseJSON_CheckIfUsernameExists(data, errorMsg: &_errorMsg)
DispatchQueue.main.async {
// USE YOUR PASSWORD AND ERROR MESSAGE HERE, E.G.:
self.passwordFromDb = password
self.errorMsg = _errorMsg
// INITIATE WHATEVER UI UPDATE YOU WANT HERE
}
}
task.resume()
} catch _ {
print ("Oops something happened buddy")
errorMsg = "Usarname non recuperato (1)"
}
}

Getting a value from web call in Swift [duplicate]

This question already has answers here:
Returning data from async call in Swift function
(13 answers)
Closed 4 years ago.
I am trying to get the returned value from a PHP script in Swift. However, it seems as though I keep getting the error:
Unexpectedly found nil while unwrapping an Optional value
Here is the class:
var value: String!
func run(idNumber: Int) {
let request = NSMutableURLRequest(url: URL(string: "https://mywebsite.com/file.php")!)
request.httpMethod = "POST"
let postString = "a=Hello"
request.httpBody = postString.data(using: String.Encoding.utf8)
let task = URLSession.shared.dataTask(with: request as URLRequest) {
data, response, error in
if error != nil {
//answer = error;
}
let answerString = String(data: data!, encoding: String.Encoding.utf8)
self.value = answerString
}
task.resume()
}
func getValue() -> String{
return value
}
The error occurs when calling the getValue() function. However, when I print out the "answerString" as soon as it is created, it prints out successfully!
The functions are called here:
let access = ApiAccess()
access.run(idNumber: 0)
print(access.getValue())
Making a request is an asynchronous task. You need to wait the closure callback to be call before calling getValue.
You can add a closure callback to your run method. That way you will know when the request has finished and you can print the result:
var value: String!
func run(idNumber: Int, #escaping callback: () -> Void) {
let request = NSMutableURLRequest(url: URL(string: "https://mywebsite.com/file.php")!)
request.httpMethod = "POST"
let postString = "a=Hello"
request.httpBody = postString.data(using: String.Encoding.utf8)
let task = URLSession.shared.dataTask(with: request as URLRequest) {
data, response, error in
if error != nil {
//answer = error;
}
let answerString = String(data: data!, encoding: String.Encoding.utf8)
self.value = answerString
callback()
}
task.resume()
}
func getValue() -> String{
return value
}
let access = ApiAccess()
access.run(idNumber: 0) {
print(access.getValue())
}
The issue is that the callback for URLSession.shared.dataTask() happens asynchronously, so you'll end up executing access.getValue() before your var value is ever set. This means that value is forcefully unwrapped before it has a value, which causes this error.
To workaround this, consider using promises, RxSwift, or similar async tools so that you only access values when available.
Refactor your run(idNumber:) function to take a completion handler:
func run(idNumber: Int, completion: (String?, Error?)-> Void ) {
let request = NSMutableURLRequest(url: URL(string: "https://mywebsite.com/file.php")!)
request.httpMethod = "POST"
let postString = "a=Hello"
request.httpBody = postString.data(using: String.Encoding.utf8)
let task = URLSession.shared.dataTask(with: request as URLRequest) {
data, response, error in
if error != nil {
completion(nil, error)
}
let answerString = String(data: data!, encoding: String.Encoding.utf8)
self.value = answerString
completion(answerString, nil)
}
task.resume()
}
And call it like this:
let access = ApiAccess()
access.run(idNumber: 0) { result, error in
guard let result = result else {
print("No result. Error = \(error)")
return
}
print("result = \(result)")
}
(Or use Futures or Promises, as mentioned by #JohnEllmore in his answer)

Json read at swift [duplicate]

This question already has answers here:
How can I get the Data from NSURLSession.sharedSession().dataTaskWithRequest
(2 answers)
JSON parsing swift, array has no value outside NSURLSession
(1 answer)
Closed 4 years ago.
at my form load, i need to get a data from json string and push it into an arraylist. When i check my code with breakpoints, my cursor enters my method, going through, and it get task2, pass "if" then stopped at var sec="" then exit my method, after 2-3 seconds, it enters do for.
Whats wrong with my code?
override func viewDidLoad() {
super.viewDidLoad()
let urlJsonToken2 = "https://services.domain.com/"
let myURL2 = NSURL(string: urlJsonToken2)
let request2 = NSMutableURLRequest(url: myURL2! as URL)
request2.httpMethod = "GET"
request2.addValue("Bearer "+tokenNewId, forHTTPHeaderField: "Authorization")
request2.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "content-type")
request2.setValue("application/json", forHTTPHeaderField: "Accept")
let task2 = URLSession.shared.dataTask(with: request2 as URLRequest) {(data2, response2, error2) -> Void in
if let unwrappedData2 = data2 {
do {
guard let records = try? JSONSerialization.jsonObject(with: unwrappedData2, options: .mutableContainers) as? [[String: Any]] else {
return
}
for item in records! {
let id = item["id"] as? intmax_t
let name = item["name"] as? String
self?.ArrayList.append(ClassCat(id:id!, id: name!, name));
}
} catch {}
}
} task2.resume()
var sec = ""
}

How to solve this issue "Ambiguous reference to member 'dataTask(with:completionHandler:)' " [duplicate]

This question already has answers here:
Swift 3 URLSession.shared() Ambiguous reference to member 'dataTask(with:completionHandler:) error (bug)
(14 answers)
Closed 5 years ago.
How to solve this issue
Ambiguous reference to member 'dataTask(with:completionHandler:)'
guard let requestUrl = URL(string:"http://www.digi.com/laravel_api_demo/api/demoapi") else { return }
let request = NSMutableURLRequest(url: requestUrl)
request.httpMethod = "POST"
let postString = "firstName=James&lastName=Bond"
request.httpBody = postString.data(using: String.Encoding.utf8)
let task = URLSession.shared.dataTask(with: request){ (data, response, error) in
if error != nil
{
//print(error)
return
}
//You can print out response object
print("response = \(response)")
//Print out response body
let responseString = String(data: data, encoding: NSUTF8StringEncoding)
print("response data = \(responseString)")
var err: Error?
var json = JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? NSDictionary
if let parseJSON = json {
var firstNameValue = parseJSON["firstName"] as? String
print("first name value = \(firstNameValue)")
}
}
task.resume()
}
Call should be like this , first parameter is of type URL not NSMutableURLRequest
guard let requestUrl = URL(string:"http://www.digi.com/laravel_api_demo/api/demoapi") else { return }
var request = URLRequest(url: requestUrl)
request.httpMethod = "POST"
let postString = "firstName=James&lastName=Bond"
request.httpBody = postString.data(using: String.Encoding.utf8)
let task = URLSession.shared.dataTask(with: request){ (data, response, error) in
if error != nil
{
//print(error)
return
}
}
task.resume()
see here signature of available methods

How to parse data from server? [duplicate]

This question already has answers here:
Correctly Parsing JSON in Swift 3
(10 answers)
Closed 5 years ago.
I am getting data like {"OTP":"5480"} in a string named responseString, How can I uset it.
My Code is.
#IBAction func signupButton() {
var request = URLRequest(url: URL(string:"http://122.166.215.8:8090/RESTMVC/validateMobileNumber")!)
request.httpMethod = "POST"
let mobileNumberString : String = self.mobileNumberTextfield.text!
let postString = ["mobileNumber":mobileNumberString]
request.httpBody = try! JSONSerialization.data(withJSONObject: postString, options:.prettyPrinted)
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data, error == nil else { // check for fundamental networking error
print("error=\(error)")
return
}
if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 { // check for http errors
print("statusCode should be 200, but is \(httpStatus.statusCode)")
print("response = \(response)")
}
let responseString = String(data: data, encoding: .utf8)
var recived = [UInt8]()
recived.append(contentsOf: data)
print(responseString!)
DispatchQueue.main.async(execute: {
self.performSegue(withIdentifier: "OTPView", sender: nil)
});
}
task.resume()
}
and I want to change that string into Array. Or is there any way in which I can get Array directly on the place of String?
To access value of "OTP" you need to parse your response string and convert it in Json dictionary you can achive this using following code, just pass your response data in JSONSerialization.jsonObject(with: data, options: .allowFragments)
do {
let json = try JSONSerialization.jsonObject(with: data, options: .allowFragments) as! Dictionary<String, Any>
if let otpValue = json["OTP"] {
print("Otp value : \(otpValue)")
}
} catch {
// Handle Exception
}
You can get otp from dictionary like below.
print(responseString["OTP"])

Resources