0

I'm trying to use alamofire to work with an api on a website. And so far, ive gotten it to give a response, but whenever I do, I keep getting a scope error.

Here is the code. I'm trying to get "response" out of there and into a variable

AF.request("\(website)/favorites.json", method: .get, headers: headers).responseString { response in
    debugPrint("\(response)")
}

After the function, I tried putting print(response) after the function so it looked like this:

AF.request("\(website)/favorites.json", method: .get, headers: headers).responseString { response in
    debugPrint("\(response)")
}
print(response)

But I get this error:

error: MyPlayground.playground:30:7: error: cannot find 'response' in scope
print(response)
      ^~~~~~~~

How do I pass the response out of the function.

  • The function works asynchronously, what's wrong to proceed inside the closure? Or use a completion handler as described [here](https://stackoverflow.com/questions/25203556/returning-data-from-async-call-in-swift-function). And I'm sure the next major update of AF will support `async/await` – vadian Oct 06 '21 at 13:44
  • `response` exists only between `{ response in` and `}`, not outside, that's the "scope". Now, managing asynchrone is a other concept, see the linked question in the previous comment. – Larme Oct 06 '21 at 13:46

0 Answers0