当前位置:网站首页>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).
边栏推荐
猜你喜欢
随机推荐
【Unity3D】初学加密技巧(反破解)
PanGu-Coder: A function-level code generation model
Ansible learning summary (11) - detailed explanation of forks and serial parameters of task parallel execution
工程师如何对待开源 --- 一个老工程师的肺腑之言
HCIP 第八天
HCIP第二天
解决IDEA安装安装插件慢问题
有点奇怪!访问目的网址,主机能容器却不行
原型模式
MySQL常见索引类型
HCIP 第六天
Biotinyl Cystamine | CAS: 128915-82-2 | biotin cysteamine
A young man with strong blood and energy actually became a housekeeper. How did he successfully turn around and change careers?
Appium swipe problem
18、优化网站性能
2022-7-31 12点 程序爱生活 恒指底背离中,有1-2周反弹希望
QT web 开发 - 笔记 - 3
HCIP 第十天
[Unity3D] Beginner Encryption Skills (Anti-Cracking)
优炫数据库的逻辑复制怎么样?









![MFC最详细入门教程[转载]](/img/5d/655e75f6b8a547fefe0137a9b11536.png)