oc中的blocks的功能,一种比代理简洁的方式
blocks方式:
谁要东西谁就要写blocks,通过blocks(返回值)获取想要的,提供东西的人要通过实现函数指针把东西给想要的人.当然也可以传值,传值需要通过形参.
block的功能:
block功能就是通过一个方法的返回值来获取其他类中的内容,或者设置其他类中的值, 总的来说就是操作其他类,其他类调用block时,可以具有一些灵活性,但是必须符合方法规范(即block定义的方法的形式).
上面是个人对blocks的理解;接下来是自己写的一个简单的例子模拟,卖方-快递-买方,之间的交互,大体结构是:卖方通过blocks将货物交给快递,快递将货物送到客户手中客户将钱给快递员.
/**********************************************************************************************/
/**********************************************************************************************/
首先是main.m文件
//买方向卖方提交商品信息
Buyer *buyer1=[[Buyer alloc] init];
[buyer1 setGoods:@"apple1"];
[buyer1 setName:@"buyer1"];
Buyer *buyer2=[[Buyer alloc] init];
[buyer2 setGoods:@"apple2"];
[buyer2 setName:@"buyer2"];
Buyer *buyer3=[[Buyer alloc] init];
[buyer3 setGoods:@"apple3"];
[buyer3 setName:@"buyer3"];
Buyer *buyer4=[[Buyer alloc] init];
[buyer4 setGoods:@"apple4"];
[buyer4 setName:@"buyer4"];
NSArray *arrGoods=@[[buyer1 goods],[buyer2 goods],[buyer3 goods],[buyer4 goods]];
//买方规定价钱
Seller *seller=[[Seller alloc] init];
[seller setGoods:[buyer1 goods]];
[seller setBuyerName:[buyer1 name]];
[seller setMoney:@"12"];
[seller setGoods:[buyer2 goods]];
[seller setBuyerName:[buyer2 name]];
[seller setMoney:@"34"];
[seller setGoods:[buyer3 goods]];
[seller setBuyerName:[buyer3 name]];
[seller setMoney:@"135"];
[seller setGoods:[buyer4 goods]];
[seller setBuyerName:[buyer4 name]];
[seller setMoney:@"543"];
//将价格和商品存入数组发送给快递
NSArray *arrBuyer=[[NSArray alloc] initWithArray:[seller buyerName]];
NSArray *arrMoney=[[NSArray alloc] initWithArray:[seller money]];
Poster *poster=[[Poster alloc] init];
[poster setBuyerList:arrBuyer];
[poster setGoodsList:arrGoods];
[poster setGoodsMoney:arrMoney];
[seller setSeller:poster];
//快递把商品交给买方
[poster setPoster:buyer1];
sleep(1);
[poster setPoster:buyer2];
sleep(1);
[poster setPoster:buyer3];
sleep(1);
[poster setPoster:buyer4];
sleep(1);
接下来是各个类的具体实现:
/**********************************************************************************************/
Seller.h
/**********************************************************************************************/
#import <Foundation/Foundation.h>
#import "Poster.h"
@interface Seller : NSObject
{
Poster *_poster;
NSMutableArray *_buyername; //保存买方的姓名
NSMutableArray *_goods; //保存买方的商品
NSMutableArray *_money; //保存买方的商品价格
}
-(void)setBuyerName:(NSString *)name;
-(NSMutableArray *)buyerName;
-(void)setGoods:(NSString *)goods1;
-(NSMutableArray *)goods;
-(void)setMoney:(NSString *)money1;
-(NSMutableArray *)money;
-(void)setSeller:(Poster *)poster; //回调方法,将送货单(姓名,商品,价格)传给快递
@end
/**********************************************************************************************/
Seller.m
/**********************************************************************************************/
#import "Seller.h"
@implementation Seller
-(id)init
{
if(self=[super init])
{
_buyername = [[NSMutableArray alloc] init];
_goods=[[NSMutableArray alloc] init];
_money=[[NSMutableArray alloc] init];
}
return self;
}
-(void)setBuyerName:(NSString *)name
{
[_buyername addObject:name];
}
-(NSMutableArray *)buyerName
{
return _buyername;
}
-(void)setGoods:(NSString *)goods1
{
[_goods addObject:goods1];
}
-(NSMutableArray *)goods
{
return _goods;
}
-(void)setMoney:(NSString *)money1
{
[_money addObject:money1];
}
-(NSMutableArray *)money
{
return _money;
}
//调用Poster中的方法,让poster回调Seller获取要送的东西(包含价格和商品,还有顾客信息)
-(void)setSeller:(Poster *)poster
{
[poster sendGoods:^NSMutableArray *(void) {
NSMutableArray *dic=[[NSMutableArray alloc] init];
[dic addObject:_buyername];
[dic addObject:_goods];
[dic addObject:_money];
NSLog(@"\n卖家把货单给了快递\n货单如下:");
NSMutableString *ms=[[NSMutableString alloc] init];
for(int i=0;i<[[dic objectAtIndex:0] count];i++)
{
[ms appendFormat:@"\n"];
for(int j=0;j<[dic count];j++)
{
[ms appendFormat:@"%@ ",[[dic objectAtIndex:j] objectAtIndex:i]];
}
}
NSLog(@"%@",ms);
return dic;
}];
}
@end
/**********************************************************************************************/
Poster.h
/**********************************************************************************************/
#import <Foundation/Foundation.h>
#import "Buyer.h"
@interface Poster : NSObject
{
Buyer *_buyer;
NSMutableArray *_buyerList; //保存买方的姓名
NSMutableArray *_goodsList; //保存买方的商品
NSMutableArray *_goodsMoney; //保存买方的商品价格
}
-(void)setBuyerList:(NSArray *)buyerList;
-(NSMutableArray *)buyList;
-(void)setGoodsList:(NSArray *)goods;
-(NSMutableArray *)goodsList;
-(void)setGoodsMoney:(NSArray *)money;
-(NSMutableArray *)goodsMoney;
-(void)sendGoods:(NSMutableArray *(^)(void))eachSend; //建立blocks,供外界访问
-(void)setPoster:(Buyer *)buyer; //回调方法,传送商品和价格信息
@end
/**********************************************************************************************/
Poster.m
/**********************************************************************************************/
#import "Poster.h"
@implementation Poster
-(id)init
{
if(self =[super init])
{
_goodsList=[[NSMutableArray alloc] init];
_goodsMoney=[[NSMutableArray alloc] init];
}
return self;
}
-(void)setBuyerList:(NSMutableArray *)buyerList
{
_buyerList=buyerList;
}
-(NSMutableArray *)buyList
{
return _buyerList;
}
-(void)setGoodsList:(NSMutableArray *)goods
{
_goodsList = goods;
}
-(NSMutableArray *)goodsList
{
return _goodsList;
}
-(void)setGoodsMoney:(NSMutableArray *)money
{
_goodsMoney = money;
}
-(NSMutableArray *)goodsMoney
{
return _goodsMoney;
}
//调用buyer中的方法,让buyer回调poster获取要给的钱数
-(void)setPoster:(Buyer *)buyer
{
static int i=-1;
_buyer = buyer;
[buyer reciveGoods:^NSString *(void) {
i++;
return [[NSString alloc]initWithFormat:@"%@ %@",[_goodsMoney objectAtIndex:i],[_goodsList objectAtIndex:i]];
}];
}
//回调Seller类中获取货物发送给顾客
-(void)sendGoods:(NSMutableArray *(^)(void))eachSend
{
NSMutableArray *mArr=[[NSMutableArray alloc]initWithArray:eachSend()];
NSArray *buyerArray=[mArr objectAtIndex:0];
NSArray *goodsArray=[mArr objectAtIndex:1];
NSArray *moneyArray=[mArr objectAtIndex:2];
_buyerList=[[NSMutableArray alloc] initWithArray: buyerArray];
_goodsMoney=[[NSMutableArray alloc] initWithArray: moneyArray];
_goodsList=[[NSMutableArray alloc] initWithArray:goodsArray];
// //postBlocks=[afterEachSend copy];
for(int i=0;i<[[mArr objectAtIndex:1] count];i++)
{
NSLog(@"货物%@已经发出.",[goodsArray objectAtIndex:i]); //快递发送完商品后返回商品名给商家
sleep(1);
}
}
@end
Buyer.h
#import <Foundation/Foundation.h>
@interface Buyer : NSObject
@property (nonatomic,copy) NSString *name; //保存用户姓名
@property (nonatomic,copy) NSString *goods; //保存购买的商品
-(void)reciveGoods:(NSString *(^)(void))afterEachRecive; //建立blocks,供外界访问
@end
/**********************************************************************************************/
Buyer.m
/**********************************************************************************************/
#import "Buyer.h"
@implementation Buyer
-(id)init
{
if(self=[super init])
{
_name=@"";
_goods=@"";
}
return self;
}
//Buyer回调Poster获取要给的钱数
-(void)reciveGoods:(NSString *(^)(void))afterEachRecive
{
NSArray *arr=[afterEachRecive() componentsSeparatedByString:@" "];
NSLog(@"%@收到商品后,为%@支付了%@",[self name], arr[1], arr[0]);//买家接收商品后返回钱
}
@end
/**********************************************************************************************/
代码运行结果
/**********************************************************************************************/
2015-08-16 21:19:46.276 Asignment_1[758:24272]
卖家把货单给了快递
货单如下:
2015-08-16 21:19:46.316 Asignment_1[758:24272]
buyer1 apple1 12
buyer2 apple2 34
buyer3 apple3 135
buyer4 apple4 543
2015-08-16 21:19:46.316 Asignment_1[758:24272] 货物apple1已经发出.
2015-08-16 21:19:47.321 Asignment_1[758:24272] 货物apple2已经发出.
2015-08-16 21:19:48.324 Asignment_1[758:24272] 货物apple3已经发出.
2015-08-16 21:19:49.327 Asignment_1[758:24272] 货物apple4已经发出.
2015-08-16 21:19:50.333 Asignment_1[758:24272] buyer1收到商品后,为apple1支付了12
2015-08-16 21:19:51.334 Asignment_1[758:24272] buyer2收到商品后,为apple2支付了34
2015-08-16 21:19:52.340 Asignment_1[758:24272] buyer3收到商品后,为apple3支付了135
2015-08-16 21:19:53.341 Asignment_1[758:24272] buyer4收到商品后,为apple4支付了543