-1

I'm building an iOS app in Swift 3 that's supposed to communicate with a JSON Rest Api that I'm also building myself. The app will get all sorts of content from the Api, but for now all I need it to do is check the availability of the Api through a handshake function.

let task = URLSession.shared.dataTask(with: url) { (data, response, error) in

        if error != nil {

            print(error)

        } else {

            if let urlContent = data {

                do {

                    let jsonResult = try JSONSerialization.jsonObject(with: urlContent, options: JSONSerialization.ReadingOptions.mutableContainers) as! NSDictionary

                    if jsonResult["response"] as! String == "Welcome, come in!" {
                        print("************ RESPONSE IS: ************")
                        print(jsonResult)
                        return

                    } else {
                        return
                    }

                } catch {

                    print("************ JSON SERIALIZATION ERROR ************")

                }

            }

        }
    }

    task.resume()

This is the dataTask I've set up and it runs just fine (When I print the jsonResult, I get the "Welcome!" message as expected. The problem is that I want my handshake function to return true or false (So that I can give an alert if the case is false.) When I try to set up a return true or false within the if-statement, I get the error: Unexpected non-void return value in void function.

My question is: How do I return the data out of the dataTask so that I can perform checks with it within my handshake function? I'm very new to Swift so all help is appreciated :)

Below is the entire class:

import Foundation

class RestApiManager: NSObject {

var apiAvailability:Bool?

func handshake() -> Bool {

    let url = URL(string: "https://api.restaurapp.nl/handshake.php")!

    let task = URLSession.shared.dataTask(with: url) { (data, response, error) in

        if error != nil {

            print(error)

        } else {

            if let urlContent = data {

                do {

                    let jsonResult = try JSONSerialization.jsonObject(with: urlContent, options: JSONSerialization.ReadingOptions.mutableContainers) as! NSDictionary

                    if jsonResult["response"] as! String == "Welcome, come in!" {
                        print("************ RESPONSE IS: ************")
                        print(jsonResult)
                        return true

                    } else {
                        return false
                    }

                } catch {

                    print("************ JSON SERIALIZATION ERROR ************")

                }

            }

        }
    }

    task.resume()


}

}

  • 1
    Possible duplicate of [Returning data from async call in Swift function](http://stackoverflow.com/questions/25203556/returning-data-from-async-call-in-swift-function) – vadian Aug 31 '16 at 11:34
  • Possible duplicate of [How can I get the Data from NSURLSession.sharedSession().dataTaskWithRequest](http://stackoverflow.com/questions/31264172/how-can-i-get-the-data-from-nsurlsession-sharedsession-datataskwithrequest) – Eric Aya Aug 31 '16 at 12:19

1 Answers1

0

Because you're using an asynchronous API, you can't return the bool from your handshake function. If you want to show an alert in the false case, you would replace the return false with something like:

DispatchQueue.main.async {
    self.showAlert()
}

Technically you could make the handshake function pause until the network stuff was done, and return the bool, but that defeats the purpose of being asynchronous, and it would freeze your app's UI during the network activity, so I doubt that's what you want to do.

Rob N
  • 15,024
  • 17
  • 92
  • 165