将AFNetworking swift 2.3 mock转换为swift 3?

时间:2016-10-11 21:32:58

标签: swift afnetworking swift3

我们正在尝试将swift2.3 AFNetworking模拟转换为swift 3.以下是Swift 2.3代码:

class MockAFHTTPRequestOperationManager : AFHTTPRequestOperationManager {

    var capturedPOSTURL = ""
    var capturedPOSTParameters: Dictionary<String, String> = [:]

    internal override func post(_ URLString: String!, parameters: AnyObject!, success: ((AFHTTPRequestOperation?, AnyObject?) -> Void)!, failure: ((AFHTTPRequestOperation?, NSError?) -> Void)!) -> AFHTTPRequestOperation! {
        capturedPOSTURL = URLString
        capturedPOSTParameters = parameters as! Dictionary
        return AFHTTPRequestOperation()
    }
}

这是我们尝试的swift 3代码,但是当我们在func POST前面添加覆盖时出现了编译错误:

    func POST(URLString: String, parameters: AnyObject, success: (_ operation: AFHTTPRequestOperation, _ responseObject: AnyObject) -> Void, failure: (_ operation: AFHTTPRequestOperation, _ error: NSError) -> Void) -> AFHTTPRequestOperation {

        return AFHTTPRequestOperation()

    }

它表示该方法不会覆盖其超类中的任何方法。

这是我们试图覆盖的Objective-C方法:

- (AFHTTPRequestOperation *)POST:(NSString *)URLString
                      parameters:(id)parameters
                         success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
                         failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure;

我们正在使用AFNetworking 2.5.1

我们如何使用swift3语法正确覆盖AFHTTPRequestOperationManager POST方法?

1 个答案:

答案 0 :(得分:1)

这有效:

    override func post(_ URLString: String, parameters: Any, success: ((_ operation: AFHTTPRequestOperation, _ responseObject: Any) -> Void)! , failure: ((_ operation: AFHTTPRequestOperation, _ error: Error) -> Void)!) -> AFHTTPRequestOperation {

        return AFHTTPRequestOperation()

    }