@onExit
@onExit {
@strongify(self);
expect(self).notTo.beNil();
};
在工程里可以看到有两处使用@onExit的地方例如上面示例,那么这个onExit的作用是什么,原理是什么?
先从原理讲起,看一下onExit的宏定义,
#ifndef onExit
#define onExit \
sd_keywordify \
#看这里,核心代码行#
__strong sd_cleanupBlock_t metamacro_concat(sd_exitBlock_, __LINE__) __attribute__((cleanup(sd_executeCleanupBlock), unused)) = ^
#endif
typedef void (^sd_cleanupBlock_t)(void);
#if defined(__cplusplus)
extern "C" {
#endif
void sd_executeCleanupBlock (__strong sd_cleanupBlock_t *block);
#if defined(__cplusplus)
}
#endif
核心代码行分分三部分:
-
__strong sd_cleanupBlock_t
很好理解,声明了一个strong类型的block。 -
metamacro_concat(sd_exitBlock_, __LINE__)
本质是利用宏##
连接特性将sd_exitBlock_与LINE合并。宏声明如下
/**
* Returns A and B concatenated after full macro expansion.
*/
#define metamacro_concat(A, B) \
metamacro_concat_(A, B)
#define metamacro_concat_(A, B) A ## B
-
__attribute__((cleanup(sd_executeCleanupBlock), unused)) = ^
这些代码使用了编辑器属性__attribute
,用作向编译器描述特殊的标识、检查或优化。简单讲就是给编译器加一段代码,在这里这段代码是cleanup
。下面就讲一下__attribute__((cleanup(...)))
的黑魔法。
attribute((cleanup(...)))
基本用法
__attribute__((cleanup(...)))
,用于修饰一个变量,在它的作用域结束时可以自动执行一个指定的方法,如:
// 指定一个cleanup方法,注意入参是所修饰变量的地址,类型要一样
// 对于指向objc对象的指针(id *),如果不强制声明__strong默认是__autoreleasing,造成类型不匹配
static void stringCleanUp(__strong NSString **string) {
NSLog(@"%@", *string);
}
// 在某个方法中:
{
__strong NSString *string __attribute__((cleanup(stringCleanUp))) = @"sunnyxx";
} // 当运行到这个作用域结束时,自动调用stringCleanUp
所谓作用域结束,包括大括号结束、return、goto、break、exception等各种情况。
当然,可以修饰的变量不止NSString,自定义Class
或基本类型
都是可以的:
// 自定义的Class
static void sarkCleanUp(__strong Sark **sark) {
NSLog(@"%@", *sark);
}
__strong Sark *sark __attribute__((cleanup(sarkCleanUp))) = [Sark new];
// 基本类型
static void intCleanUp(NSInteger *integer) {
NSLog(@"%d", *integer);
}
NSInteger integer __attribute__((cleanup(intCleanUp))) = 1;
假如一个作用域内有若干个cleanup的变量,他们的调用顺序是先入后出
的栈式顺序;
而且,cleanup是先于这个对象的dealloc
调用的。
进阶用法
既然__attribute__((cleanup(...)))
可以用来修饰变量,block
当然也是其中之一,写一个block的cleanup函数非常有趣:
// void(^block)(void)的指针是void(^*block)(void)
static void blockCleanUp(__strong void(^*block)(void)) {
(*block)();
}
于是在一个作用域里声明一个block:
{
// 加了个`unused`的attribute用来消除`unused variable`的warning
__strong void(^block)(void) __attribute__((cleanup(blockCleanUp), unused)) = ^{
NSLog(@"I'm dying...");
};
} // 这里输出"I'm dying..."
这里不得不提万能的Reactive Cocoa
中神奇的@onExit
方法,其实正是上面的写法,简单定义个宏:
#define onExit\
__strong void(^block)(void) __attribute__((cleanup(blockCleanUp), unused)) = ^
用这个宏就能将一段写在前面的代码最后执行:
{
onExit {
NSLog(@"yo");
};
} // Log "yo"
这样的写法可以将成对出现的代码写在一起,比如说一个lock:
NSRecursiveLock *aLock = [[NSRecursiveLock alloc] init];
[aLock lock];
// 这里
// 有
// 100多万行
[aLock unlock]; // 看到这儿的时候早忘了和哪个lock对应着了
用了onExit
之后,代码更集中了:
NSRecursiveLock *aLock = [[NSRecursiveLock alloc] init];
[aLock lock];
onExit {
[aLock unlock]; // 妈妈再也不用担心我忘写后半段了
};
// 这里
// 爱多少行
// 就多少行
还是那句老话:剩下的就全靠想象力了。
看到这里有没有联想到swift里有个defer
的操作😁。
这个宏的作用就是类似defer
在代码scope结束时执行一些操作。
网友评论