10

I created a function to get URL from API, and return URL string as the result. However, Xcode gives me this error message:

Unexpected non-void return value in void function

Does anyone know why this happens?

func getURL(name: String) -> String {

        let headers: HTTPHeaders = [
            "Cookie": cookie
            "Accept": "application/json"
        ]

        let url = "https://api.google.com/" + name

        Alamofire.request(url, headers: headers).responseJSON {response in
            if((response.result.value) != nil) {
                let swiftyJsonVar = JSON(response.result.value!)

                print(swiftyJsonVar)

                let videoUrl = swiftyJsonVar["videoUrl"].stringValue

                print("videoUrl is " + videoUrl)

                return (videoUrl)   // error happens here
            }
        }
}
Wei Xia
  • 504
  • 2
  • 9
  • 24
  • 2
    The return statement is returning a value as a result of that completion handler (closure), not the `getURL(name:)` function. The completion handler isn't expected to return anything. – Alexander May 11 '17 at 18:36
  • You should look up the concept of asynchronous functions. They operate with completion blocks like this one, instead of returning things. – Connor Neville May 11 '17 at 18:37
  • you need to use completion handler to return value from the closure. – Shabir jan May 11 '17 at 18:37

2 Answers2

17

Use closure instead of returning value:

func getURL(name: String, completion: @escaping (String) -> Void) {
    let headers: HTTPHeaders = [
        "Cookie": cookie
        "Accept": "application/json"
    ]
    let url = "https://api.google.com/" + name
    Alamofire.request(url, headers: headers).responseJSON {response in
        if let value = response.result.value {
            let swiftyJsonVar = JSON(value)
            print(swiftyJsonVar)
            let videoUrl = swiftyJsonVar["videoUrl"].stringValue
            print("videoUrl is " + videoUrl)
            completion(videoUrl)
        }
    }
}

getURL(name: ".....") { (videoUrl) in
    // continue your logic
}
Vasilii Muravev
  • 3,063
  • 17
  • 45
1

You cant return value from inside closer so you need to add closure to your function

func getURL(name: String , completion: @escaping (_ youstring : String) -> (Void) ) -> Void {

            let headers: HTTPHeaders = [
                "Cookie": cookie
                "Accept": "application/json"
            ]

            let url = "https://api.google.com/" + name

            Alamofire.request(url, headers: headers).responseJSON {response in
                if((response.result.value) != nil) {
                    let swiftyJsonVar = JSON(response.result.value!)

                    print(swiftyJsonVar)

                    let videoUrl = swiftyJsonVar["videoUrl"].stringValue

                    print("videoUrl is " + videoUrl)
                     completion (youstring :  )
                      // error happens here
                }
            }
    }
KKRocks
  • 8,222
  • 1
  • 18
  • 84