-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()


}

}

4

1 に答える 1

0

非同期 API を使用しているため、関数から bool を返すことはできませんhandshake。false の場合にアラートを表示する場合は、 を次のreturn falseように置き換えます。

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

技術的には、ネットワークの処理が完了するまでハンドシェイク機能を一時停止して bool を返すこともできますが、それは非同期の目的を無効にし、ネットワーク アクティビティ中にアプリの UI をフリーズさせるので、それがあなたのやりたいことだとは思えません。 .

于 2016-08-31T11:48:16.297 に答える