Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

typealias closure error in swift

I am using AFNetworking in a Swift project.

1 - Typealias the closure:

typealias successBlock = (AFHTTPRequestOperation! ,AnyObject!)-> Void
typealias failureBlock = (AFHTTPRequestOperation, NSError!) -> Void

2 - Define the function:

func getUserInfo(success: (successBlock)!, failure: (failureBlock)!) {
   let path = "https://api.wei.s.json"
   let parameters = ["source":"key"]
   self.GET(path, parameters: parameters, success: success, failure: failure)
}

3 - Error:

Cannot invoke 'GET' with an argument list of type '(String,parameters: [String : String], success: (successBlock)!, failure: (failureBlock)!)'

Thanks for any help.

EIDT:

typealias failureBlock = (AFHTTPRequestOperation, NSError!) -> Void

to

typealias failureBlock = (AFHTTPRequestOperation!, NSError!) -> Void
like image 433
William Hu Avatar asked Apr 24 '15 08:04

William Hu


2 Answers

I think it you are missing a ! on the failureBlock typealias definition it is expecting an explicitly unwrapped optional AFHTTPRequestOperation not a standard AFHTTPRequestOperation which are actually different types. I believe it should be,

typealias failureBlock = (AFHTTPRequestOperation!, NSError!) -> Void
like image 152
Neil Horton Avatar answered Nov 18 '22 23:11

Neil Horton


Try that:

func getUserInfo(success: (successBlock)!, failure: (failureBlock)!) {
   let path = "https://api.wei.s.json"
   let parameters = ["source":"key"]
   self.GET(path, parameters: parameters, success: success!, failure: failure!)
}

(Note the two "bang" ! on the arguments success and failure.

That's based on the assumption that the GET function expects a closure and not an optional referring to a closure. It might be it. Otherwise, use the keystroke to get method completion on self.GET and see the type that Swift expects. It will tell you where there is an issue.

like image 1
Laurent Michel Avatar answered Nov 19 '22 00:11

Laurent Michel