Iphone 使用Blocks

本文介绍了iOS编程中Block的三种实用场景:循环迭代运算、类型定义简化及电影播放后的自动队列管理。通过具体代码实例展示了如何利用Block提高代码效率和可读性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

格式说明:void (^now)( void ) 格式: 返回值(^名称)参数

 

先主要发一些代码,等有时间再具体说明

 

代码一 : 主要是循环返回 i与3的积,并打印

 

.h文件

@interface Worker : NSObject {
}
 
+ (void)iterateFromOneTo:(int)limit withBlock:(int (^)(int))block;
 
@end

 .m文件

@implementation Worker
 
+ (void)iterateFromOneTo:(int)limit withBlock:(int (^)(int))block {
    for (int i = 1; i <= limit; i++) {
        int result = block(i);
        NSLog(@"iteration %d => %d", i, result);
    }
}
 
@end

 调用时:

[Worker iterateFromOneTo:5 withBlock:^(int number) {
    return number * 3;
}];

 也可以这样:

int (^tripler)(int) = ^(int number) {
    return number * 3;
};
 
[Worker iterateFromOneTo:5 withBlock:tripler];

 结果:

iteration 1 => 3
iteration 2 => 6
iteration 3 => 9
iteration 4 => 12
iteration 5 => 15

 代码二: 使用TypeDef 简化代码,使代码更可读

.h文件

typedef int (^ComputationBlock)(int);
 
@interface Worker : NSObject {
}
 
+ (void)iterateFromOneTo:(int)limit withBlock:(ComputationBlock)block;
 
@end

 .m文件

@implementation Worker
 
+ (void)iterateFromOneTo:(int)limit withBlock:(ComputationBlock)block {
    for (int i = 1; i <= limit; i++) {
        int result = block(i);
        NSLog(@"iteration %d => %d", i, result);
    }
}
 
@end

 代码三 : 当播放时,自动删除已经播放的名称

.h文件

typedef void (^MoviePlayerCallbackBlock)(NSString *);
 
@interface MoviePlayer : NSObject {
}
 
@property (nonatomic, copy) MoviePlayerCallbackBlock callbackBlock;
 
- (id)initWithCallback:(MoviePlayerCallbackBlock)block;
- (void)playMovie:(NSString *)title;
 
@end

 .m文件

#import "MoviePlayer.h"
 
@implementation MoviePlayer
 
@synthesize callbackBlock;
 
- (id)initWithCallback:(MoviePlayerCallbackBlock)block {
    if (self = [super init]) {
        self.callbackBlock = block;
    }
    return self;
}
 
- (void)playMovie:(NSString *)title {
    // play the movie
    self.callbackBlock(title);
}
 
- (void)dealloc {
    [callbackBlock release];
    [super dealloc];
}
 
@end

 引用代码

NSMutableArray *movieQueue =
    [NSMutableArray arrayWithObjects:@"Inception",
                                     @"The Book of Eli",
                                     @"Iron Man 2",
                                     nil];
 
MoviePlayer *player =
    [[MoviePlayer alloc] initWithCallback:^(NSString *title) {
        [movieQueue removeObject:title];
}];
 
for (NSString *title in [NSArray arrayWithArray:movieQueue]) {
    [player playMovie:title];
};

 

Notice that the block uses the local movieQueue variable, which becomes part of the state of the block. When the block is called it removes the movie title from the movieQueue array even though it’s out of scope by that time. After all the movies have been played, the movieQueue will be empty.

 

参考链接:http://pragmaticstudio.com/blog/2010/9/15/ios4-blocks-2

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值