当前位置:网站首页>OC-块对象
OC-块对象
2022-08-09 11:08:00 【彭同学她同桌】
返回值(^block名称)(形参列表)//返回值可以省略
相当于函数指针
块语句并不用于在内存中分配的块对象 只是编写代码时的一种表达用语。
block可以存在栈区 堆区 全局区
block刚创建出来的时候是栈区
block只能调用外部全局变量 不能修改
例子
void(^block)() = ^{
//block实现
NSLog(@"block")
};
block();
int(^func)(int a,int b)
{
return a+b;
}
int temp = func(10,20);
//等价于
int temp = ^(int a,int b)
{
return a+b;
}(10,20)
block也可以作为形参
(返回值类型(^)(形参列表))形参名称
(void(^)())blick
例子
@interface A:NSObject<NSCoding>
-(void)funcA:(void(^)(int num))block;
//如果多个block则 -(void)funcA:(void(^)(int num))block oth:(void(^)(BOOL isSucc) block1);
@end
@implementation A
-(void)funcA:(void(^)(int num))block{
//表明参数是一个(void(^)(int num))类型的形参 参数名叫block
NSLog(@"%s",__func__);
block(3);
}
@end
@interface B : NSObject
-(void)funcB;
@end
@implementation B
-(void)funcB{
NSLog(@"%s",__func__);
};
@end
int main(int argc, const char * argv[]) {
@autoreleasepool {
A *a = [[A alloc]init];
[a funcA:^(int num){
//如果没有参数(int num)都可以省略 不需要括号
B *b = [[B alloc]init];
[b funcB];
}];
}
return 0;
}
通过typedef简化
typedef 返回值类型(^block别名)(形参列表)
typedef void (^Block1)(int num);
typedef void(^Block2)(BOOL b);
typedef void (^Block1)(int num);
typedef void(^Block2)(BOOL b);
//原来
-(void)funcA:(void(^)(int num))block funcA1:(void(^)(BOOL b)block2);
//简化
-(void)funcA:(Block1)block funcA1:(Block2)block2;
边栏推荐
- Create a table in a MySQL database through Doc
- For versions corresponding to tensorflow and numpy, report FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecate
- activemq 消息持久化
- torch.stack()的官方解释,详解以及例子
- pip common commands and changing source files
- 【C language】动态数组的创建和使用
- PTA 找出不是两个数组共有的元素
- 华为VRRP+MSTP联动接口检测实验案例
- golang源代码阅读,sync系列-Pool
- 1005 Spell It Right (20分)
猜你喜欢

Qt获取EXE可执行文件的上一级目录下的文件

数论知识点

Tensorflow realize parameter adjustment of linear equations

MATLAB代码实现三次样条插值

Preparation for gold three silver four: how to successfully get an Ali offer (experience + interview questions + how to prepare)

activemq 消息持久化

jmeter BeanShell 后置处理器

去除蜂窝状的噪声(matlab实现)

FreeRTOS列表和列表项源码分析

美的数字化平台 iBUILDING 背后的技术选型
随机推荐
Numpy常用操作博客合集
focusablejs
SQL Server查询优化
性能测试(04)-表达式和业务关联-JDBC关联
sublime记录
1006 Sign In and Sign Out (25分)
PTA习题 分类统计字符个数(C)
PTA 矩阵运算
Tensorflow realize parameter adjustment of linear equations
The torch. The stack () official explanation, explanation and example
fork创建多个子进程
Open3D 点云平均点间距评估
学长告诉我,大厂MySQL都是通过SSH连接的
torch.stack()的官方解释,详解以及例子
性能测试(01)-jmeter元件-线程组、调试取样器
∘(空心的点乘)的数学含义
PTA 找出不是两个数组共有的元素
golang源代码阅读,sync系列-Pool
Multi-merchant mall system function disassembly 26 lectures - platform-side distribution settings
PTA 指定位置输出字符串(c)