Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何调用包含块作为参数和NSString作为块参数的方法_Ios_Objective C Blocks - Fatal编程技术网

Ios 如何调用包含块作为参数和NSString作为块参数的方法

Ios 如何调用包含块作为参数和NSString作为块参数的方法,ios,objective-c-blocks,Ios,Objective C Blocks,我有一个这样的方法 -(void)GetPostPreperation :(NSMutableURLRequest *)request :(BOOL)isGet :(NSMutableDictionary *)jsonBody :(void(^)(NSString*)) compblock 如何在一个块内将参数传递到该文件中?我试过了。但它给了我一个语法错误,预期表达式 这就是我尝试的方式 [self GetPostPreperation:request :isGet :jsonBody :

我有一个这样的方法

-(void)GetPostPreperation :(NSMutableURLRequest *)request :(BOOL)isGet :(NSMutableDictionary *)jsonBody :(void(^)(NSString*)) compblock
如何在一个块内将参数传递到该文件中?我试过了。但它给了我一个语法错误,
预期表达式

这就是我尝试的方式

 [self GetPostPreperation:request :isGet :jsonBody :(^NSString*)str
 {
     return str;

 }];
这就是我定义块的方式

typedef void(^myCompletion)(NSString *);
我想为我的
getpostprestation
方法中的block参数分配一个
NSString
值,并检查它,然后从block调用方法返回它。我该怎么做?请帮帮我。
谢谢

以这种方式使用它,这里的
str
是一个输入,不返回

[self GetPostPreperation:request :true : jsonBody :^(NSString * str) {
        //here use
        NSLog(@"%@",str);

    }];
更新


不要从此块返回任何内容。块类型为
void(^)(NSString*)
,返回为void

以这种方式使用它,此处的
str
是一个输入而不是返回

[self GetPostPreperation:request :true : jsonBody :^(NSString * str) {
        //here use
        NSLog(@"%@",str);

    }];
[self GetPostPreperation:nil :YES :nil :^(NSString * string) {
  //your code
  NSLog(@"%@", string);
}];
更新

不要从此块返回任何内容。块类型为
void(^)(NSString*)
,返回为void

[self GetPostPreperation:nil :YES :nil :^(NSString * string) {
  //your code
  NSLog(@"%@", string);
}];
我还建议您更改方法定义,如下所示:

-(void)GetPostPreperation :(NSMutableURLRequest *)request
                          :(BOOL)isGet :(NSMutableDictionary *)jsonBody
                          :(void(^)(NSString* string)) compblock 
这将在键入此方法时为您提供自动建议,您只需按enter键即可创建它

我还建议您更改方法定义,如下所示:

-(void)GetPostPreperation :(NSMutableURLRequest *)request
                          :(BOOL)isGet :(NSMutableDictionary *)jsonBody
                          :(void(^)(NSString* string)) compblock 
这将在键入此方法时为您提供自动建议,您只需按enter键即可创建它


只需使用字符串参数创建块,并将该块传递给方法

如下

void (/*Block name*/^failure)(/*block formal parameter*/NSError *) = ^(/*block actual parameter*/NSError *error) {
        NSLog(@"Error is:%@",error);
    };

[self myMethod:failure];

只需使用字符串参数生成块并将该块传递给方法

如下

void (/*Block name*/^failure)(/*block formal parameter*/NSError *) = ^(/*block actual parameter*/NSError *error) {
        NSLog(@"Error is:%@",error);
    };

[self myMethod:failure];

在块中声明变量名,以便在使用该块(字符串)时可以访问它

当您的方法有多个参数时,它将始终按如下方式编写(即isGet:(BOOL)isGet),因此调用该方法很容易

//分块法

    -(void)GetPostPreperation :(NSMutableURLRequest *)request isGet:(BOOL)isGet jsonBody:(NSMutableDictionary *)jsonBody block:(myCompletion)string
    {
        string(@"yes");

    }

在块中声明变量名,以便在使用该块(字符串)时可以访问它

当您的方法有多个参数时,它将始终按如下方式编写(即isGet:(BOOL)isGet),因此调用该方法很容易

//分块法

    -(void)GetPostPreperation :(NSMutableURLRequest *)request isGet:(BOOL)isGet jsonBody:(NSMutableDictionary *)jsonBody block:(myCompletion)string
    {
        string(@"yes");

    }

您想要的意思是您询问如何使用typedef或其他东西您想要的意思是您询问如何使用typedef或其他东西来获得答案,但对我来说,它给出了一个类似这样的错误:不兼容的块指针类型将“NSString*(^)(NSString*u_ustrong)”发送到“void(^)类型的参数(NSString*\uuu strong)'发布关于如何使用此函数的完整代码,我猜您从该块中返回了一些内容。块返回是无效的。您不需要返回。我认为他需要为块使用typedef,我是对的,或者我被误解了?您好,回答如下,但对我来说,它给出了一个错误,如发送'NSString*(^)的不兼容块指针类型(NSString*_____________________________________________________________________(NSString*\uuuuu strong)'到'void(^)(NSString*\uuuu strong)'类型的参数我收到了这个错误。为什么这/为什么你要从
void
块返回字符串?如果你想这样做,你需要将返回类型不兼容的块指针类型发送'NSString*(^)(NSString*\uu strong)'到'void(^)(NSString*\uu strong)类型的参数'我收到此错误。为什么/为什么您要从
void
块返回字符串?如果需要,您需要更改返回类型