0

我试图弄清楚如何声明一个将块作为参数并仅从外部范围记录整数值的方法。我看到的大多数示例都是在某些 Apple API 上执行此操作,indexesOfObjectsPassingTest:但我只想创建自己的简单版本。这就是我所拥有的,目前无法正常工作:

@interface IAViewController ()
+(void)tell2:(void(^)(void)) thisBlock;
@end

...

NSInteger someInt=289456;
[IAViewController tell2:^{
    NSLog(@"what is this? %i", someInt);
}];

// ? how do I make this method signature work
+(void) tell2:(void (^thisBlock)) myInt{
    thisBlock(myInt);
}

如何使方法签名参数正常工作以输出 289456?

4

2 回答 2

3

当您将块类型声明为Objective-C方法的参数时,块的标识符在类型之外。所以语法看起来像这样:

@interface IAViewController ()
+(void)tell2:(void(^)(void)) thisBlock;
@end

@implementation IAViewController
- (void)someMethod {
    NSInteger someInt=289456;
    [IAViewController tell2:^{
        NSLog(@"what is this? %i", someInt);
    }];
}

+(void) tell2:(void (^)(void))thisBlock {
    thisBlock();
}
@end
于 2013-08-11T19:57:41.397 回答
0

你的问题是你有文本myInt,你应该有块名称,然后你正在调用一个块,它带有一个没有在任何地方声明的参数的 void 参数。

你有@interface正确的方法声明。在 中再次使用它@implementation并丢弃对 的所有引用myInt

+(void)tell2:(void(^)(void)) thisBlock
{
    // your method implementation
}
于 2013-08-11T20:02:05.983 回答