当前位置:网站首页>Three types of [OC learning notes] Block
Three types of [OC learning notes] Block
2022-08-02 08:29:00 【Billy Miracle】
Block的三种类型
block的类型,取决于isa指针,可以通过调用class方法或者isa指针查看具体类型,最终都是继承自NSBlock类型.
使用如下代码:
void testBlockType(void) {
int age = 10;
void(^block1)(void) = ^{
NSLog(@"block1----");
};
void(^block2)(void) = ^{
NSLog(@"block2----%d", age);
};
NSLog(@"block1-----%@ %@ %@ %@", [block1 class], [[block1 class] superclass], [[[block1 class] superclass] superclass], [[[[block1 class] superclass] superclass] superclass]);
NSLog(@"block2-----%@ %@ %@ %@", [block2 class], [[block2 class] superclass], [[[block2 class] superclass] superclass], [[[[block2 class] superclass] superclass] superclass]);
NSLog(@"block-----%@ %@ %@ %@", [^{
NSLog(@"block----%d", age);
} class], [[^{
NSLog(@"block----%d", age);
} class] superclass], [[[^{
NSLog(@"block----%d", age);
} class] superclass] superclass], [[[[^{
NSLog(@"block----%d", age);
} class] superclass] superclass] superclass]);
}
打印结果:
三个block的类型分别为:__NSGlobalBlock__、__NSMallocBlock__、__NSStackBlock__The above three types are ultimately inherited fromNSBlock,而NSBlock又是继承自NSObject:further explained hereblock其实就是一个OC对象.
- NSMallocBlock(_NSConcreteMallocBlock) 对象存储在堆区
- NSStackBlock(_NSConcreteStackBlock) 对象存储在栈区
- NSGlobalBlock(_NSConcreteGlobalBlock)对象存储在数据区
换到MRCEnvironment to try:
MRC模式下,三种block类型:__NSGlobalBlock__、__NSStackBlock__、__NSStackBlock__,Why is the type in the middle given bymalloc变成了stack?这是因为ARCautomatically help usblock进行了copy操作.
分析
Let's try to convert firstC++Code view type:
struct __testBlockType_block_impl_0 {
struct __block_impl impl;
struct __testBlockType_block_desc_0* Desc;
__testBlockType_block_impl_0(void *fp, struct __testBlockType_block_desc_0 *desc, int flags=0) {
impl.isa = &_NSConcreteStackBlock;
impl.Flags = flags;
impl.FuncPtr = fp;
Desc = desc;
}
};
struct __testBlockType_block_impl_1 {
struct __block_impl impl;
struct __testBlockType_block_desc_1* Desc;
int age;
__testBlockType_block_impl_1(void *fp, struct __testBlockType_block_desc_1 *desc, int _age, int flags=0) : age(_age) {
impl.isa = &_NSConcreteStackBlock;
impl.Flags = flags;
impl.FuncPtr = fp;
Desc = desc;
}
};
struct __testBlockType_block_impl_2 {
struct __block_impl impl;
struct __testBlockType_block_desc_2* Desc;
int age;
__testBlockType_block_impl_2(void *fp, struct __testBlockType_block_desc_2 *desc, int _age, int flags=0) : age(_age) {
impl.isa = &_NSConcreteStackBlock;
impl.Flags = flags;
impl.FuncPtr = fp;
Desc = desc;
}
};
发现都是_NSConcreteStackBlock类型.cannot respond correctlyblock的实质类型(可能是LLVMCompiler version problem,而clang又是LLVM的一部分).
In addition to the heap area, the program partition requires the programmer to manually manage memory,Other areas are automatically managed by the system.
{
//等号左边:autoshape local variable,存放在栈区;
//等号右边:常量,存放在数据区;
int age = 10;
//等号左边:autoshape local variable(指针),存放在栈区;
//等号右边:autoshape local variable address,存放在栈区;
int *agePtr = &age;
//等号左边:autoshape local variable(指针),存放在栈区;
//等号右边:allocopen object,存放在堆区;
NSObject *objc = [[NSObject alloc] init];
}
三种block类型(global、malloc、stack),从字面理解,Can be concluded that, in turn, stored in a data area、堆区、栈区;blcok1没有访问任何变量,后两个blockboth access variablesage,而age是一个auto类型的局部变量.
int height = 150;
void testBlockType(void) {
int age = 10;
static int weight = 80;
void(^block1)(void) = ^{
NSLog(@"block1----");
};
void(^block2)(void) = ^{
NSLog(@"block2----%d", age);
};
void(^block3)(void) = ^{
NSLog(@"-----%d", weight);
};
void(^block4)(void) = ^{
NSLog(@"-----%d", height);
};
NSLog(@"%@ %@", [block3 class], [block4 class]);
}
结果:
如果是static修饰的局部变量,or access global variables,则block的类型都是__NSGlobalBlock__,then it is almost certain,blockThe type can depend on the properties of the variable it accesses.
因为autoLocal variables of type are stored in the stack area,而blockto access the variable,After the previous analysis,blockwill capture the variable inblock结构体内部,Is to create a memory to store the local variable(相当于copy操作,但不是copy),那么此时的blockWhere do you store yourself in??前面说了,autoLocal variables of type must be stored in the stack area,这点毋庸置疑,而blockAlthough newly opened memory to store the variable,But can't change the variable is aautoproperties of local variables of type,因此此时的blockcan only be stored in the stack area;Since it is stored in the stack area,then the scope of the variable accessed is limited to the scope of the nearest brace,If it exceeds, it will be automatically released.
MRC下测试下面代码:
void(^block4)(void);
void test3()
{
int age = 10;
block4 = ^{
NSLog(@"----%d", age);
};
}
int main(int argc, const char * argv[]) {
@autoreleasepool {
test3();
block4();
// NSLog(@"%@", [block4 class]);
}
return 0;
}
输出:
说明ageHas been released automatically,block再次调用时,Access to discarded memory.手动copy:
void(^block4)(void);
void test3()
{
int age = 10;
block4 = [^{
NSLog(@"----%d", age);
} copy];
}
int main(int argc, const char * argv[]) {
@autoreleasepool {
test3();
block4();
NSLog(@"%@", [block4 class]);
}
return 0;
}
结果:
因为copy是把ageThe value is directly copied to a new memory area,而我们知道copyCreate a memory must be in the heap area operations(同时,blockThe type is changed from the previous__NSStackBlock__类型变为__NSMallocBlock__类型).因此,防止一个autoType of local variables automatically release method,就是将其copyTo the heap area manual management,To achieve the purpose of controllable life cycle(So remember to release[block release])——这是MRCManually manage memory in mode,而在ARCIn this mode, the system will automatically manage the memory(copy和release).
边栏推荐
猜你喜欢

Biotin-EDA|CAS:111790-37-5| 乙二胺生物素
![[OC学习笔记]Block三种类型](/img/40/edf59e6e68891ea7c9ab0481fe7bfc.png)
[OC学习笔记]Block三种类型

flutter 自己写一个组件
![[OC学习笔记]ARC与引用计数](/img/56/033cfc15954567d63d987d91ca8d63.png)
[OC学习笔记]ARC与引用计数

Business Intelligence Platform BI Business Intelligence Analysis Platform How to Choose the Right Business Intelligence Platform BI

解决IDEA安装安装插件慢问题

59: Chapter 5: Develop admin management services: 12: MongoDB usage scenarios; (non-core data, non-core data with a relatively large amount of data, small private files such as face photos;)

多版本node的安装与切换详细操作

Button to control the running water light (timer)

node(三) 模块化
随机推荐
R language plotly visualization: plotly visualizes the scatter plot of the actual value of the regression model and the predicted value of the regression, analyzes the prediction performance of the re
科技云报道:实现元宇宙,英伟达从打造基础建设平台开始
读入、输出优化
[Unity3D] Beginner Encryption Skills (Anti-Cracking)
优炫数据库的逻辑复制怎么样?
小说里的编程 【连载之二十二】元宇宙里月亮弯弯
Biotin - LC - Hydrazide | CAS: 109276-34-8 | Biotin - LC - Hydrazide
Redisson distributed lock source code analysis for high-level use of redis
如何开启mysql慢查询日志?
PanGu-Coder: A function-level code generation model
[OC学习笔记]weak的实现原理
商业智能平台BI 商业智能分析平台 如何选择合适的商业智能平台BI
uniapp 禁止默认返回事件
MGRE综合实验
Biotin hydrazide HCl|CAS:66640-86-6|Biotin-hydrazide hydrochloride
typescript学习
HCIP 第四天
AcWing 2811. 最长公共子串(后缀自动机 fa 指针的性质)
工程师如何对待开源 --- 一个老工程师的肺腑之言
【Unity3D】初学加密技巧(反破解)