•Block的定义
•Block、委托、通知、回调函数,它们虽然名字不一样,但是原理都一样,都是“回调机制”的思想的具体实现
•前面的代理模式的项目改为Block回调实现
Block回调案例 1
•typedef long(^LookforApartme)(Person *person);
•
•@property(nonatomic)LookforApartme lookforApartme;
•
•long price=_lookforApartme(self);
•
•Person *person=[[Person alloc] init];
•
•[person lookforApartmeBlock:^long(Person *person) {
• long price = random()%10 * 1000;
• NSLog(@"正在努力的找房子,找到一个价格为%ld的房子",price);
• return price;
• }];
•
Block回调案例2
•typedef void (^WashBlock)(Children *children);
•typedef void (^PlayBlock)(Children *children);
•
•@property(nonatomic)WashBlock washBlock;
•@property(nonatomic)PlayBlock playBlock;
•
• self.washBlock(self);
• self.playBlock(self);
KVO,代理,Block回调的区别
•KVO:可以是一对一或者一对多
•代理:只能是一对一
•Block回调块:代码以闭包得形式将各种内容进行传递