当前位置:网站首页>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;
边栏推荐
- b站up主:空狐公子 --矩阵求导(分母布局)课程笔记
- 为什么组合优先于继承
- faster-rcnn学习
- gdb 10.2的安装
- Preparation for gold three silver four: how to successfully get an Ali offer (experience + interview questions + how to prepare)
- Since I use the HiFlow scene connector, I don't have to worry about becoming a "dropper" anymore
- CentOS6.5 32bit安装Oracle、ArcSde、Apache等配置说明
- Qt获取EXE可执行文件的上一级目录下的文件
- 【Subpixel Dense Refinement Network for Skeletonization】CVPR2020论文解读
- C语言统计不同单词数
猜你喜欢
随机推荐
RPN principle in faster-rcnn
图片查看器viewer
【C language】typedef的使用:结构体、基本数据类型、数组
golang源代码阅读,sync系列-Pool
Solve 1. tensorflow runs using CPU but not GPU 2. GPU version number in tensorflow environment 3. Correspondence between tensorflow and cuda and cudnn versions 4. Check cuda and cudnn versions
乘积量化(PQ)
cnn的输入输出
PTA 指定位置输出字符串(c)
threejs+shader 曲线点运动,飞线运动
程序员的专属浪漫——用3D Engine 5分钟实现烟花绽放效果
Oracle数据库体系结构
Tensorflow realize parameter adjustment of linear equations
美的数字化平台 iBUILDING 背后的技术选型
x86异常处理与中断机制(2)中断向量表
golang runtime Caller、Callers、CallersFrames、FuncForPC、Stack作用
vite的原理,手写vite
focusablejs
七夕?程序员不存在的~
centos7.5 设置Mysql开机自启动
PTA习题 阶梯电价(C)









