I am writing a Service class that will get data from a network layer and passes it to ViewModel. I have used completion handlers to pass data from the network layer. Now I am not sure how to pass this data from the Network layer to my view model. Following is my code in the Service layer:
class HomeService {
func getStates() -> [State]? {
let urlString = baseURL + statesURL
let params = ["user_id": UserDetails.getUserID() as AnyObject]
NetworkOperations.fetchPostResponse(url: urlString, param: params, completionHandler: { (result) in
switch(result) {
case .Success(let res):
guard let response = res as? [String: AnyObject] else {
return
}
if let status = response["status"] as? Bool,
status,
let content = response["content"] as? [[String: AnyObject]] {
let array = content.map({ State(json: $0) })
return array
//error here: Unexpected non-void return value in void function
}
break
case .Failure(_):
break
}
})
//error here: Missing return in a function expected to return '[State]?'
}
}
I have tried above but I am getting error:
Missing return in a function expected to return '[State]?'
And inside the handler where I try to return the array I get the following error:
Unexpected non-void return value in void function