我应该在块中使用函数参数,还是将它们保留为 __block 变量
Should I use function parameters in block, or keep them as __block variables
当我在一个块中使用实例变量时,最好的做法是在它之前添加__block,这样作用域是异步的,之后我就可以使用它了。
示例:
-(void) someFunction {
__block NSString *nameIWillUseInBlock = @"Some name"; //Here ill use the __block
[self functionWithCallback:^(NSString *string) {
NSString *stringWithString = [NSString stringWithFormat:@"%@%@", nameIWillUseInBlock, @"with bla"];
//Here Ill do somthing with this string
}];
}
所以我的问题是,传递给函数的参数怎么样,我可以像这样使用它们吗:
-(void) someFunctionWithParam:(NSString *) nameIWillUseInBlock {
[self functionWithCallback:^(NSString *string) {
NSString *stringWithString = [NSString stringWithFormat:@"%@%@", nameIWillUseInBlock, @"with bla"];
//Here Ill do somthing with this string
}];
}
或者我应该将参数保留为 __block 实例,如下所示:
-(void) someFunctionWithParam:(NSString *) nameIWillUseInBlock {
__block NSString *asyncNameIWillUseInBlock = nameIWillUseInBlock;
[self functionWithCallback:^(NSString *string) {
NSString *stringWithString = [NSString stringWithFormat:@"%@%@", asyncNameIWillUseInBlock, @"with bla"];
//Here Ill do somthing with this string
}];
}
如果我应该使用参数,而不是作为 __block 实例,作用域如何知道 参数在当前作用域之前传递?
谢谢。
你可以直接在块中使用参数你可以使用这段代码。
-(void) someFunctionWithParam:(NSString *) nameIWillUseInBlock {
[self functionWithCallback:^(NSString *string) {
NSString *stringWithString = [NSString stringWithFormat:@"%@%@", nameIWillUseInBlock, @"with bla"];
//Here Ill do somthing with this string
}];
}
这是关于该主题的非常好的苹果文档 Working With Blocks
我认为 "Blocks Can Capture Values from the Enclosing Scope" 部分正是您要查找的内容。
"在此示例中,在块外声明了一个整数,但在定义块时捕获了该值。
仅捕获值,除非您另有说明。这意味着如果您在定义块和调用它的时间之间更改变量的外部值
所以如果你看这个:
int anInteger = 42;
void (^testBlock)(void) = ^{
NSLog(@"Integer is: %i", anInteger);
};
anInteger = 84;
testBlock();
日志会显示
Integer is: 42
"Use __block Variables to Share Storage"
__block int anInteger = 42;
void (^testBlock)(void) = ^{
NSLog(@"Integer is: %i", anInteger);
};
anInteger = 84;
testBlock();
日志会显示
Integer is: 84
当我在一个块中使用实例变量时,最好的做法是在它之前添加__block,这样作用域是异步的,之后我就可以使用它了。 示例:
-(void) someFunction {
__block NSString *nameIWillUseInBlock = @"Some name"; //Here ill use the __block
[self functionWithCallback:^(NSString *string) {
NSString *stringWithString = [NSString stringWithFormat:@"%@%@", nameIWillUseInBlock, @"with bla"];
//Here Ill do somthing with this string
}];
}
所以我的问题是,传递给函数的参数怎么样,我可以像这样使用它们吗:
-(void) someFunctionWithParam:(NSString *) nameIWillUseInBlock {
[self functionWithCallback:^(NSString *string) {
NSString *stringWithString = [NSString stringWithFormat:@"%@%@", nameIWillUseInBlock, @"with bla"];
//Here Ill do somthing with this string
}];
}
或者我应该将参数保留为 __block 实例,如下所示:
-(void) someFunctionWithParam:(NSString *) nameIWillUseInBlock {
__block NSString *asyncNameIWillUseInBlock = nameIWillUseInBlock;
[self functionWithCallback:^(NSString *string) {
NSString *stringWithString = [NSString stringWithFormat:@"%@%@", asyncNameIWillUseInBlock, @"with bla"];
//Here Ill do somthing with this string
}];
}
如果我应该使用参数,而不是作为 __block 实例,作用域如何知道 参数在当前作用域之前传递?
谢谢。
你可以直接在块中使用参数你可以使用这段代码。
-(void) someFunctionWithParam:(NSString *) nameIWillUseInBlock {
[self functionWithCallback:^(NSString *string) {
NSString *stringWithString = [NSString stringWithFormat:@"%@%@", nameIWillUseInBlock, @"with bla"];
//Here Ill do somthing with this string
}];
}
这是关于该主题的非常好的苹果文档 Working With Blocks 我认为 "Blocks Can Capture Values from the Enclosing Scope" 部分正是您要查找的内容。
"在此示例中,在块外声明了一个整数,但在定义块时捕获了该值。
仅捕获值,除非您另有说明。这意味着如果您在定义块和调用它的时间之间更改变量的外部值
所以如果你看这个:
int anInteger = 42;
void (^testBlock)(void) = ^{
NSLog(@"Integer is: %i", anInteger);
};
anInteger = 84;
testBlock();
日志会显示
Integer is: 42
"Use __block Variables to Share Storage"
__block int anInteger = 42;
void (^testBlock)(void) = ^{
NSLog(@"Integer is: %i", anInteger);
};
anInteger = 84;
testBlock();
日志会显示
Integer is: 84