在Objective-C块中遇到NSString返回类型有麻烦 [英] Having trouble with NSString return type in Objective-C blocks

查看:104
本文介绍了在Objective-C块中遇到NSString返回类型有麻烦的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

感谢您的帮助.我的问题是下一个.我需要使用AFNetworking,并且在向服务器请求请求时,服务器是响应.这个结果我需要在我的方法中返回.方法AFNetworking使用成功或失败的块.我想以成功或失败的选项块返回返回服务器字符串(操作).

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;
    }];
}

但是显示错误:

发送'NSString的不兼容块指针类型 *(^)(AFHTTPRequestOperation * __ strong,_ strong id)'到类型为void(^)(AFHTTPRequestOperation * _strong,__strong id)'"

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

对于我的问题很愚蠢,我感到抱歉,因为我开始使用iOS进行开发.请帮我!!!谢谢.

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

推荐答案

实际上这是async block,并且您和方法将在块完全执行之前返回. 您不应返回此string,而应调用一些完成处理程序或委托方法. 更好的方法是将完成处理程序传递给方法,并在返回字符串的地方调用该完成处理程序.

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
}];

这篇关于在Objective-C块中遇到NSString返回类型有麻烦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆