Bojan Radulovic Bojan Radulovic - 3 months ago 14
Swift Question

Cannot convert value of type 'Response<AnyObject, NSError>' to closure result type 'NSDictionary'

class AlamofireService {


static let alamofireService = AlamofireService()


private init() {

}

internal func makePostServiceRequest(url: String, parameters: AnyObject, completion: (inner: () throws -> NSDictionary) -> Void) -> Void{


Alamofire.request(.POST, URL_BASE, parameters: [IOS_PARAMS : parameters], encoding:.JSON).validate()
.responseJSON

{
response in switch response.result
{
case .Success(let JSON):
print("Success with JSON: \(JSON)")


let response = JSON as! NSDictionary
print(response)

completion(inner: { return response })



case .Failure(let error):
print("Request failed with error: \(error)")
completion(inner: { throw error })

}
}
}



internal func makeGetServiceRequestTemplates(completion: (inner: () throws -> NSDictionary) -> Void) ->Void {

Alamofire.request(.GET, URl_TEMPLATES).validate().responseJSON

{
response in switch response.result
{
case .Success(let JSON):
print("Success with JSON: \(JSON)")



if let array = JSON as? [Dictionary<String, AnyObject>] {

for var i=0; i < array.count; i++ {

let templates = Mapper<VQuizTemplateTo>().map(array[i])
print(templates)

}


}
completion(inner: { return response }) //error is on this line


case .Failure(let error):
print("Request failed with error: \(error)")
completion(inner: { throw error })
}

}
}


Two identical methods, for the first there is no errors and for the second is on place as i marked. Can't figure out what is wrong, especially with this non sense error.
Could you tell me what i'm doing such wrong?

Answer

The response you are returning is of typeResponse<AnyObject, NSError>, which is why you are getting that error..

Try returning this instead response.result.value as! NSDictionary

Just replace completion(inner: { return response }) with

completion(inner: { return response.result.value as! NSDictionary })