①
#pragma mark 使用typedef来声明block
typedef int (^BlockType) (void);
typedef void (^BlockType2)(void);
typedef int (^BlockType3)(int a, int b);
②
//自定义一个compare方法,通过每次两个对象之间的排序实现数组的排序
NSInteger compare(Student* stu1, Student* stu2, void *context)
{
if (stu1.age > stu2.age) {
return NSOrderedDescending;
}
else if (stu1.age < stu2.age) {
return NSOrderedAscending;
}
return NSOrderedSame;
}
③
对于非静态变量来说:
1. 没有用__block修饰的变量,复制成只读,并且复制发生在block对象定义的时候
2. 用__block修饰的变量,复制成可读写的变量,并且复制发生在调用的时候
3. block对象如果复制的是对象类型,它不但会复制指针的值,而且会连它的属性(strong, weak)一起复制
__weak ViewController *mySelf = self 解决引用循环,不改变self的引用计数,定义一个弱引用的指针指向self
和前一个方法功能一样,这个比较常用 __weak typeof(self) mySelf = self
block的一些规则和定义block的方法
最新推荐文章于 2021-08-04 03:21:41 发布