Search code examples
iosswiftswift3ios10afhttprequestoperation

Swift error Cannot convert value of type '(AFHTTPRequestOperation?, AnyObject?) -> ()


After I update xcode Version 8.0 (8A218a) swift 3, I got this error

Cannot convert value of type '(AFHTTPRequestOperation?, AnyObject?) -> ()' to expected argument type '((AFHTTPRequestOperation?, Any?) -> Void)!'

This is the following code that's shown error above.

jsonmanager.post( "http://myapi.com",
                      parameters: nil,
                      success: { (operation: AFHTTPRequestOperation!,responseObject: AnyObject!) in
                        if(responseObject.object(forKey: "meta")?.object(forKey: "status")?.intValue == 200){....

Am i doing something wrong ?

It works well in previous version 7.3.1 swift 2.


Solution

  • The callback method signature has changed. In Swift 2 it was

    (AFHTTPRequestOperation?, AnyObject?) -> Void
    

    In Swift 3 it's

    (AFHTTPRequestOperation?, Any?) -> Void
    

    You should change the line below

    success: { (operation: AFHTTPRequestOperation!,responseObject: AnyObject!)
    

    To

    success: { (operation: AFHTTPRequestOperation?, responseObject: Any?)