Objective C块和Swift Closure

时间:2014-08-01 09:23:27

标签: objective-c swift objective-c-blocks

我想将以下内容转换为swift。

- (AFHTTPRequestOperation *)GOQIIGET:(NSString *)URLString
parameters:(id)parameters
success:(void (^)(id responseObject))success
failure:(void (^)(NSError *error))failure
{
    return [self GET:URLString parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
    success(responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    failure(error);
    }];
}

由于

1 个答案:

答案 0 :(得分:0)

我没有在Swift项目中包含AFNetworking,所以我无法查看已翻译的标题。您必须检查类型,下面的代码肯定不会与它们开箱即用。但无论如何,这是一个想法:

func GOQIIGET(
    urlString: String?,
    parameters: AnyObject?,
    success: (( responseObject: AnyObject? ) -> Void)?,
    failure: (( error: NSError? ) -> Void)?
    ) -> AFHTTPRequestOperation?
{
    return self.GET( urlString, parameters: parameters,
        success: { ( operation: AFHTTPRequestOperation?, responseObject: AnyObject? ) in
            if let success = success {
                success( responseObject: responseObject )
            }
        },
        failure: { ( operation: AFHTTPRequestOperation?, error: NSError? ) in
            if let failure = failure {
                failure( error: error )
            }
        })
}