0

I'm a beginner to Swift and Alamofire. I have a very simple GET request where I just want to decode the data passed back into a custom class, and return that class, but I'm stuck with the completion/response handler bit, and my knowledge of this area isn't great.

func getUser() -> UserResponse? {
     AF.request("\(baseUrl)/Users/GetCurrent",
               method: .get,
               headers: baseHeaders)
      .validate(statusCode: 200..<300)
      .responseDecodable(of: UserResponse.self) { response in
        
      switch response.result {
        case .success:
          guard let userResponse = response.value else {
            return
          }
          // Does not work of course
          return userResponse
        case let .failure(error):
          print(error)
          return
      }
    }
  }

I can't return the value I need from inside this completion, so how do I get what I want outside of it and return it, or nil if something went wrong? The ideal usage for this function would be something like let user = apiService.getUser(), I don't want to have to deal with passing completions or json or whatever outside of this function.

Thanks, I'm sure this is a noob question

0 Answers0