//block聲明:返回值(^block變量名)(參數(shù))
void(^block)();
//block定義:三種方式 = ^(參數(shù)){};
//第一種
void(^block1)(void) = ^{
NSLog(@"調(diào)用了block1");
};
//第二種:block 如果沒(méi)有參數(shù)暂幼,參數(shù)可以隱藏证逻,如果有參數(shù)偿渡,定義的時(shí)候气堕,必須寫參數(shù)峻呕,而且必須要有參數(shù)變量名
void(^block2)(int) =^(int a){
};
//第三種 block返回值可以省略堆缘,不管有沒(méi)有返回值范嘱,都可以省略
int(^block3)(void) = ^{
return 3;
};
//同上
int(^block4)(void) = ^int{
return 3;
};
//block 類型:int(^)(NSString *) 返回值省略了
int(^block5)(NSString *) = ^(NSString *name){
return 4;
};
//block 調(diào)用
block1();
block2(3);
block3();
block4();
block5(@"haha");
//場(chǎng)景1:在一個(gè)方法中定義送膳,在另一個(gè)方法中調(diào)用 test調(diào)用
//BlockType 類型別名
typedef void(^BlockType)(void);
/**1,建立一個(gè)block 屬性 練習(xí) 功能是傳入一個(gè)String 輸出一個(gè)String */
@property (nonatomic,strong)NSString *(^block1)(NSString *);
//定義一個(gè)block
NSString *(^block)(NSString *)=^NSString *(NSString *a){
NSString *str = [NSString stringWithFormat:@"打印++%@",a];
NSLog(@"%@",str);
return str;
};
//賦值保存為全局屬性
_block1 = block;
//調(diào)用block 對(duì)應(yīng)第一個(gè)開(kāi)發(fā)場(chǎng)景
- (IBAction)test:(id)sender {
_block1(@"abc");
}
//場(chǎng)景2:在一個(gè)類中定義block,在另一個(gè)類中調(diào)用
1.創(chuàng)建一個(gè)模型
#import <Foundation/Foundation.h>
//BlockType 類型別名
typedef void(^BlockType)(void);
NS_ASSUME_NONNULL_BEGIN
@interface CallItem : NSObject
/** name */
@property (nonatomic,strong) NSString *name;
/** 動(dòng)作 */
@property (nonatomic,strong) void(^block)(void);
//一樣的定義block 同上
@property (nonatomic,strong) BlockType block2;
@property (nonatomic,strong) NSString *(^block3)(NSString *);
+ (instancetype)callItemWithString:(NSString *)name;
@end
NS_ASSUME_NONNULL_END
----------------------------------------
#import "CallItem.h"
@implementation CallItem
+ (instancetype)callItemWithString:(NSString *)name{
CallItem *item = [[CallItem alloc]init];
item.name = name;
return item;
}
@end
//創(chuàng)建模型并且在block 中寫入內(nèi)容 并加入數(shù)組中
CallItem *item1 = [CallItem callItemWithString:@"111"];
item1.block = ^{
NSLog(@"tap1");
};
item1.block3 = ^NSString * _Nonnull(NSString * a) {
NSLog(@"打印%@",a);
return a;
};
CallItem *item2 = [CallItem callItemWithString:@"222"];
item2.block = ^{
NSLog(@"tap2");
};
CallItem *item3 = [CallItem callItemWithString:@"333"];
item3.block = ^{
NSLog(@"tap3");
};
[self.callItems addObject:item1];
[self.callItems addObject:item2];
[self.callItems addObject:item3];
//點(diǎn)擊調(diào)用
- (IBAction)tap1:(UIButton *)sender {
CallItem *item = self.callItems[0];
item.block();
item.block3(@"tap1");
}
- (IBAction)tap2:(UIButton *)sender {
CallItem *item = self.callItems[1];
item.block();
}
- (IBAction)tap3:(UIButton *)sender {
CallItem *item = self.callItems[2];
item.block();
}
//定義一個(gè)使用block返回參數(shù)的方法
- (void)post:(id)url params:(id)paramsDic model:(ResModel *)model finishCallbackBlock:(void (^)(NSString *, ResModel *))block{
[self request:url Params:paramsDic model:model FinishCallbackBlock:^(NSString *error, ResModel *result) {
block(error,result);
} type:0];
}
/// 我的粉絲請(qǐng)求接口
/// @param currentPage 當(dāng)前頁(yè)
/// @param successBlock 成功回調(diào)
/// @param fail 失敗回調(diào)
- (void)userFansQuery:(NSInteger)currentPage withSuccess:(void (^)(NSArray *_Nonnull fansList , CatPaginator *paginator))successBlock withFail:(nullable void(^)(void))fail;
最基本 使用:
//函數(shù)Block 回調(diào)
complete:(void(^)(void))complete;
typedef為C語(yǔ)言的關(guān)鍵字,作用是為一種數(shù)據(jù)類型定義一個(gè)新名字丑蛤。這里的數(shù)據(jù)類型包括內(nèi)部數(shù)據(jù)類型(int,char等)和自定義的數(shù)據(jù)類型(struct等)叠聋。
//type
typedef void(^xxBlock)(void);
@property (copy, nonatomic) void (^actionBlock)(void);