Having trouble with NSString return type in Objective-C blocks [duplicate]

纵饮孤独 提交于 2020-01-07 04:33:06

问题


Thanks guy for help you. My problem is next. I need use AFNetworking and when request petition to server, the server is response. This result I need return in my method. The method AFNetworking use block for succes or failed. I want return the return server string (operation) in succes or failed option block.

- (NSString *)executeMultipart:(NSURL *)url
{
    __block NSString *strJSON;

    [_operationManager POST:[url absoluteString]parameters:_params
  constructingBodyWithBlock:^(id<AFMultipartFormData> formData){
        [formData appendPartWithFormData:[_params objectForKey:TAG_PHOTO] name:TAG_PHOTO];
    } success:^(AFHTTPRequestOperation *operation, id responseObject) {
        strJSON = [operation.responseString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        return strJSON;
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        strJSON = [operation.responseString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        return strJSON;
    }];
}

But show error:

"Incompatible block pointer types sending 'NSString *(^)(AFHTTPRequestOperation *__strong, _strong id)' to parameter of type 'void (^)(AFHTTPRequestOperation *_strong, __strong id)'"

I'm sorry if my problem is stupid, because I beginning develop with iOS. Please help me!!! Thanks you.


回答1:


Actually this is async block and you and method will return before block execute completely. You should not return this string you should call some completion handler or delegate method. Better will be you pass completion handler to method and call that completion handler where you are returning string.

Your method should be like this.

- (void)executeMultipart:(NSURL *)url withCompletionHandler:(void (^)(NSString*))handler
{
    __block NSString *strJSON;

    [_operationManager POST:[url absoluteString]parameters:_params
  constructingBodyWithBlock:^(id<AFMultipartFormData> formData){
        [formData appendPartWithFormData:[_params objectForKey:TAG_PHOTO] name:TAG_PHOTO];
    } success:^(AFHTTPRequestOperation *operation, id responseObject) {
        strJSON = [operation.responseString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
       handler(strJSON);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        strJSON = [operation.responseString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
       handler(strJSON);
    }];
}

from the class where you are using this method you should call this method as

[obj executeMultipart:url withCompletionHandler:^(NSString* returnString)handler{
// code here 
// do whatever you want to with return string
}];



回答2:


The POST:parameters:constructingBodyWithBlock: method wants a block that doesn't return anything.

It sounds like you're trying to return the result of an asynchronous method. This isn't generally practical. The entire point of an asynchronous method is that is doesn't happen in line with other things. You'll need to have that method take a callback of its own.



来源:https://stackoverflow.com/questions/21637876/having-trouble-with-nsstring-return-type-in-objective-c-blocks

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!