无法访问在此方法中作为参数传递的块
Unable to access a block which is passed as arguments in this method
在下面的示例中,我不知道如何访问我在方法中作为参数传递的块。我应该使用 typedef,但只有当我破坏代码时,我才能得到以下场景。所以没有使用 typedef
@interface ViewController ()
- (void(^)(void)) anotherMethodWithReturnTypeAnd: ( void ( ^ )( int ))argumentsBlock;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
void (^twoBlock)(void) = [ self anotherMethodWithReturnTypeAnd:^(int n) {
NSLog(@"%d, block without typedef",n);
}];
twoBlock();
}
- (void(^)(void)) anotherMethodWithReturnTypeAnd: ( void ( ^ )( int n) )argumentsBlock{
void (^blockToPassAsReturnType)(void) = ^{
NSLog(@"Passing this block as return type");
};
return blockToPassAsReturnType;
}
输出:
2017-09-05 00:23:08.148 DeleteThisBlockProject[657:22195] Passing this block as return type
那么我应该如何为我作为参数传递的块使用和传递值,[ self anotherMethodWithReturnTypeAnd:^(int n) {
NSLog(@"%d, 块没有 typedef",n);
}];
你没有调用方法
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
void (^twoBlock)(void) = [ self anotherMethodWithReturnTypeAnd:^(int n) {
// this will never execute because you didn't call it you just implemented it
NSLog(@"%d, block without typedef",n);
}];
twoBlock();
}
- (void(^)(void)) anotherMethodWithReturnTypeAnd: ( void ( ^ )( int n) )argumentsBlock{
void (^blockToPassAsReturnType)(void) = ^{
NSLog(@"Passing this block as return type");
};
// you should call method which you did provide to this function
argumentsBlock(yourInt)
return blockToPassAsReturnType;
}
在下面的示例中,我不知道如何访问我在方法中作为参数传递的块。我应该使用 typedef,但只有当我破坏代码时,我才能得到以下场景。所以没有使用 typedef
@interface ViewController ()
- (void(^)(void)) anotherMethodWithReturnTypeAnd: ( void ( ^ )( int ))argumentsBlock;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
void (^twoBlock)(void) = [ self anotherMethodWithReturnTypeAnd:^(int n) {
NSLog(@"%d, block without typedef",n);
}];
twoBlock();
}
- (void(^)(void)) anotherMethodWithReturnTypeAnd: ( void ( ^ )( int n) )argumentsBlock{
void (^blockToPassAsReturnType)(void) = ^{
NSLog(@"Passing this block as return type");
};
return blockToPassAsReturnType;
}
输出:
2017-09-05 00:23:08.148 DeleteThisBlockProject[657:22195] Passing this block as return type
那么我应该如何为我作为参数传递的块使用和传递值,[ self anotherMethodWithReturnTypeAnd:^(int n) { NSLog(@"%d, 块没有 typedef",n); }];
你没有调用方法
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
void (^twoBlock)(void) = [ self anotherMethodWithReturnTypeAnd:^(int n) {
// this will never execute because you didn't call it you just implemented it
NSLog(@"%d, block without typedef",n);
}];
twoBlock();
}
- (void(^)(void)) anotherMethodWithReturnTypeAnd: ( void ( ^ )( int n) )argumentsBlock{
void (^blockToPassAsReturnType)(void) = ^{
NSLog(@"Passing this block as return type");
};
// you should call method which you did provide to this function
argumentsBlock(yourInt)
return blockToPassAsReturnType;
}