block的本質(zhì)
block本質(zhì)上也是一個(gè)OC對象闷堡,它內(nèi)部也有個(gè)isa指針喉前。
block是封裝了函數(shù)調(diào)用以及函數(shù)調(diào)用環(huán)境的OC對象佳吞。
//
// Created by SK on 2020/7/7.
// Copyright ? 2020 SK. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "Person.h"
NSString *name = @"jack";
int main(int argc, const char * argv[]) {
@autoreleasepool {
int age = 20;
static int sex = 0;
Person *person1 = [[Person alloc] init];
Person *person2 = [[Person alloc] init];
Person *person3 = [[Person alloc] init];
__strong Person *strongPerson = person1;
__weak Person *weakPerson = person2;
__block Person *blockPerson = person3;
void (^block)(void) = ^{
NSLog(@"%d-%d-%@", age, sex, name);
[strongPerson run];
[weakPerson run];
[blockPerson run];
};
block();
}
return 0;
}
我們通過執(zhí)行
xcrun -sdk iphoneos clang -arch arm64 -rewrite-objc -fobjc-arc -fobjc-runtime=ios-9.0 main.m
編譯一下上述代碼苍姜,來看一下block的底層結(jié)構(gòu)
struct __block_impl {
void *isa;
int Flags;
int Reserved;
void *FuncPtr;
};
struct __Block_byref_blockPerson_0 {
void *__isa;
__Block_byref_blockPerson_0 *__forwarding;
int __flags;
int __size;
void (*__Block_byref_id_object_copy)(void*, void*);
void (*__Block_byref_id_object_dispose)(void*);
Person *__strong blockPerson;
};
struct __main_block_impl_0 {
struct __block_impl impl;
struct __main_block_desc_0* Desc;
int age;
int *sex;
Person *__strong strongPerson;
Person *__weak weakPerson;
__Block_byref_blockPerson_0 *blockPerson; // by ref
}
static void __main_block_func_0(struct __main_block_impl_0 *__cself) {
__Block_byref_blockPerson_0 *blockPerson = __cself->blockPerson; // bound by ref
int age = __cself->age; // bound by copy
int *sex = __cself->sex; // bound by copy
Person *__strong strongPerson = __cself->strongPerson; // bound by copy
Person *__weak weakPerson = __cself->weakPerson; // bound by copy
NSLog((NSString *)&__NSConstantStringImpl__var_folders_h6_tk2fn5rd5r92tqhsv84hkj540000gn_T_main_e976c4_mi_1, age, (*sex), name);
((void (*)(id, SEL))(void *)objc_msgSend)((id)strongPerson, sel_registerName("run"));
((void (*)(id, SEL))(void *)objc_msgSend)((id)weakPerson, sel_registerName("run"));
((void (*)(id, SEL))(void *)objc_msgSend)((id)(blockPerson->__forwarding->blockPerson), sel_registerName("run"));
}
static void __main_block_copy_0(struct __main_block_impl_0*dst, struct __main_block_impl_0*src) {_Block_object_assign((void*)&dst->strongPerson, (void*)src->strongPerson, 3/*BLOCK_FIELD_IS_OBJECT*/);_Block_object_assign((void*)&dst->weakPerson, (void*)src->weakPerson, 3/*BLOCK_FIELD_IS_OBJECT*/);_Block_object_assign((void*)&dst->blockPerson, (void*)src->blockPerson, 8/*BLOCK_FIELD_IS_BYREF*/);}
static void __main_block_dispose_0(struct __main_block_impl_0*src) {_Block_object_dispose((void*)src->strongPerson, 3/*BLOCK_FIELD_IS_OBJECT*/);_Block_object_dispose((void*)src->weakPerson, 3/*BLOCK_FIELD_IS_OBJECT*/);_Block_object_dispose((void*)src->blockPerson, 8/*BLOCK_FIELD_IS_BYREF*/);}
static struct __main_block_desc_0 {
size_t reserved;
size_t Block_size;
void (*copy)(struct __main_block_impl_0*, struct __main_block_impl_0*);
void (*dispose)(struct __main_block_impl_0*);
} __main_block_desc_0_DATA = { 0, sizeof(struct __main_block_impl_0), __main_block_copy_0, __main_block_dispose_0};
int main(int argc, const char * argv[]) {
/* @autoreleasepool */ { __AtAutoreleasePool __autoreleasepool;
int age = 20;
static int sex = 0;
Person *person1 = ((Person *(*)(id, SEL))(void *)objc_msgSend)((id)((Person *(*)(id, SEL))(void *)objc_msgSend)((id)objc_getClass("Person"), sel_registerName("alloc")), sel_registerName("init"));
Person *person2 = ((Person *(*)(id, SEL))(void *)objc_msgSend)((id)((Person *(*)(id, SEL))(void *)objc_msgSend)((id)objc_getClass("Person"), sel_registerName("alloc")), sel_registerName("init"));
Person *person3 = ((Person *(*)(id, SEL))(void *)objc_msgSend)((id)((Person *(*)(id, SEL))(void *)objc_msgSend)((id)objc_getClass("Person"), sel_registerName("alloc")), sel_registerName("init"));
__attribute__((objc_ownership(strong))) Person *strongPerson = person1;
__attribute__((objc_ownership(weak))) Person *weakPerson = person2;
__attribute__((__blocks__(byref))) __Block_byref_blockPerson_0 blockPerson = {(void*)0,(__Block_byref_blockPerson_0 *)&blockPerson, 33554432, sizeof(__Block_byref_blockPerson_0), __Block_byref_id_object_copy_131, __Block_byref_id_object_dispose_131, person3};
void (*block)(void) = ((void (*)())&__main_block_impl_0((void *)__main_block_func_0, &__main_block_desc_0_DATA, age, &sex, strongPerson, weakPerson, (__Block_byref_blockPerson_0 *)&blockPerson, 570425344));
((void (*)(__block_impl *))((__block_impl *)block)->FuncPtr)((__block_impl *)block);
}
return 0;
}
從上述代碼可以看出译蒂,block的底層結(jié)構(gòu)如下:
block的類型
block有3種類型,可以通過調(diào)用class方法或者isa指針查看具體類型谊却,最終都是繼承自NSBlock類型柔昼。
-
NSGlobalBlock ( _NSConcreteGlobalBlock )
沒有訪問auto變量 -
NSStackBlock ( _NSConcreteStackBlock )
訪問了auto變量 -
NSMallocBlock ( _NSConcreteMallocBlock )
NSStackBlock調(diào)用了copy
block的變量捕獲
為了保證block內(nèi)部能夠正常訪問外部的變量,block有個(gè)變量捕獲機(jī)制
- _NSConcreteGlobalBlock類型的block調(diào)動copy炎辨,無變化
- _NSConcreteStackBlock類型的block調(diào)用copy捕透,從棧復(fù)制到堆
- _NSConcreteMallocBlock類型的block調(diào)用copy, 引用計(jì)數(shù)增加
在ARC環(huán)境下,編譯器會根據(jù)情況自動將棧上的block復(fù)制到堆上碴萧,例如:
- block作為函數(shù)返回值時(shí)
- 將block賦值給
__strong
指針時(shí) - block作為Cocoa API中方法名含有usingBlock的方法參數(shù)時(shí)
- block作為GCD API的方法參數(shù)時(shí)
Auto類型變量
當(dāng)block內(nèi)部訪問了對象類型的auto變量時(shí)
- 如果block是在棧上乙嘀,將不會對auto變量產(chǎn)生強(qiáng)引用
- 如果block被拷貝到堆上,會調(diào)用block內(nèi)部的copy函數(shù)
copy函數(shù)內(nèi)部會調(diào)用_Block_object_assign函數(shù)
_Block_object_assign函數(shù)會根據(jù)auto變量的修飾符(__strong破喻、__weak虎谢、__unsafe_unretained)做出相應(yīng)的操作,形成強(qiáng)引用(retain)或者弱引用 - 如果block從堆上移除曹质,會調(diào)用block內(nèi)部的dispose函數(shù)
dispose函數(shù)內(nèi)部會調(diào)用_Block_object_dispose函數(shù)
_Block_object_dispose函數(shù)會自動釋放引用的auto變量(release)
__block修飾符
- __block可以用于解決block內(nèi)部無法修改auto變量值的問題
- __block不能修飾全局變量婴噩、靜態(tài)變量(static)
- 編譯器會將__block變量包裝成一個(gè)對象
struct __Block_byref_blockPerson_0 {
void *__isa;
__Block_byref_blockPerson_0 *__forwarding;
int __flags;
int __size;
void (*__Block_byref_id_object_copy)(void*, void*);
void (*__Block_byref_id_object_dispose)(void*);
Person *__strong blockPerson;
};
__block的內(nèi)存管理
- 當(dāng)block在棧上時(shí),并不會對__block變量產(chǎn)生強(qiáng)引用
- 當(dāng)block被copy到堆時(shí)
- 會調(diào)用block內(nèi)部的copy函數(shù)
- copy函數(shù)內(nèi)部會調(diào)用_Block_object_assign函數(shù)
- _Block_object_assign函數(shù)會對__block變量形成強(qiáng)引用(retain)
- 當(dāng)block從堆中移除時(shí)
- 會調(diào)用block內(nèi)部的dispose函數(shù)
- dispose函數(shù)內(nèi)部會調(diào)用_Block_object_dispose函數(shù)
- _Block_object_dispose函數(shù)會自動釋放引用的__block變量(release)
循環(huán)引用
__weak typeof(self) weakSelf = self;