William Hu
William Hu

Reputation: 16159

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

Upvotes: 11

Views: 909

Answers (2)

Neil Horton
Neil Horton

Reputation: 707

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

Upvotes: 5

Laurent Michel
Laurent Michel

Reputation: 405

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.

Upvotes: 1

Related Questions