在Objective-C中是否有正确的方法来编写一个没有返回值的块?我看到的所有示例都带有返回值。有人也可以解释完成块和完成块之间的区别吗?常规区块?我知道^意味着它是一个块而不是+之前(void)意味着它也是一个块?
答案 0 :(得分:2)
如果方法头具有块的参数,那么这就是它的样子:
WITH Cte AS(
SELECT *,
RN_ASC = ROW_NUMBER() OVER(PARTITION BY CAST(RegDate AS DATE) ORDER BY RegDate ASC),
RN_DESC = ROW_NUMBER() OVER(PARTITION BY CAST(RegDate AS DATE) ORDER BY RegDate DESC)
FROM Visitor
)
DELETE FROM Cte WHERE RN_ASC > 1 AND RN_DESC > 1
所以没有返回类型且没有参数的块看起来像这样:
- (void)someMethodThatTakesABlock:(returnType (^)(parameterTypes))blockName;
常规块只是一组(或组)代码。完成块是在方法完成时将执行的块。完成块是一个常规块,它只是特定于在方法结束时调用。
- (void)someMethodThatTakesABlock:(void (^)(void))blockName;
表示一个区块。方法之前的^
是类方法。
+
returnType (^blockName)(parameterTypes) = ^returnType(parameters) {...};
@property (nonatomic, copy) returnType (^blockName)(parameterTypes);
- (void)someMethodThatTakesABlock:(returnType (^)(parameterTypes))blockName;
[someObject someMethodThatTakesABlock:^returnType (parameters) {...}];
您只需将typedef returnType (^TypeName)(parameterTypes);
TypeName blockName = ^returnType(parameters) {...};
替换为returnType
。
答案 1 :(得分:1)
这是一个演示:
1,没有返回值,也没有参数:
- (void)viewDidLoad {
[super viewDidLoad];
//block
void(^myBlock)(void) = ^(void) {
NSLog(@"This is a block without parameter and returned value");
};
myBlock();
2,没有返回值且有参数:
-(void)blockWithParameterButNoReturnData
{
void(^myBlock)(int) = ^(int num) {
NSLog(@"%d",num*100);
};
myBlock(4);
}
3,有后退值并有参数: - (无效)blockWithParameterAndReturnValue
{
int (^myBlock)(int) = ^(int num) {
return num * 100;
};
int result = myBlock(2);
NSLog(@"This is a block with parameter and return value :%d",result);
}
PS:有关详细信息,请访问此网站:http://www.cnblogs.com/zhanggui/p/4656440.html