Basically, I'm having trouble returning a url in the form of a string in my API call.
I'm trying to implement abstraction by having one big function that takes a 'breed' parameter to call into its endpoint. That way I avoid writing the same exact function multiple times. The functions getMamalute(), getGolden(), etc. all pass in this parameter to get a URL so that I can display it in my View as an Image -- as you can probably tell. But I'm getting the following error 'Unexpected non-void return value in void function' at the return line in the 'getFavoriteDoggo' function. Do I need to use a completion handler? If so, how will that look like?
@Published var mamaluteImg = ""
@Published var goldenImg = ""
@Published var samoyedImg = ""
@Published var chowImg = ""
@Published var huskyImg = ""
func getMamalute() -> String{
return getFavoriteDoggo(breed: "malamute")
}
func getChowChow() -> String{
return getFavoriteDoggo(breed: "chow")
}
func getHusky() -> String{
return getFavoriteDoggo(breed: "husky")
}
func getSamoyed() -> String{
return getFavoriteDoggo(breed: "samoyed")
}
func getGoldenRetriever() -> String{
return getFavoriteDoggo(breed: "retriever/golden")
}
func getFavoriteDoggo(breed: String) -> String{
guard let url = URL(string: "https://dog.ceo/api/breed/\(breed)/images/random") else {
print("Trouble parsing url")
return ""
}
let request = URLRequest(url: url)
URLSession.shared.dataTask(with: request){ data, response, error in
if error != nil {
print((error?.localizedDescription)!)
return
}
if let data = data {
let response = try! JSON(data: data)
// let randoIndex = Int.random(in: 0...(response.count - 1))
let img = response["message"]
// print(img)
// DispatchQueue.main.async {
// self.mamaluteImg = img.string!
// }
return img.string
}
}.resume()
}
Hopefully I explained my problem clearly, if not my apologies my brain is running really low on battery juice, so I'd be more than happy to help clarify down below:)
Thanks once again!