2016-08-28 46 views
1
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 }) 
      } 

    } 
} 

兩個相同的方法的值,用於第一沒有錯誤,並用於第二是在地方,因爲我標記。無法弄清楚什麼是錯誤的,尤其是對於這種無意義的錯誤。 你能告訴我我在做什麼這樣的錯誤嗎?無法將類型「響應<AnyObject,NSError>」到閉合結果類型「的NSDictionary」

+0

更換completion(inner: { return response })什麼你得到的錯誤? – renjithr

回答

1

response你是返回是Response<AnyObject, NSError>型,這就是爲什麼你得到的是錯誤的..

,請返回這個代替response.result.value as! NSDictionary

只是

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

+0

非常感謝,它的作品!在第一個Alamofire請求中沒有錯誤,仍然沒有得到它怎麼可能,但是這很重要,這是有效的 –

+0

請求中沒有錯誤,但是你試圖分配值類型'Response '到'NSDictionary'這是你得到錯誤的原因。 – ebby94

相關問題