我旨在实现ReactiveCocoa Signal Message Forwarding的实现。
ARNSwizzBlocks的特性如下
实例方法的交换。
方法交换影响范围仅为实例对象。
这受到以下产品的启发。
需要iOS 7.0或更高版本,并使用ARC。
@property (nonatomic, weak) IBOutlet UITableView *tableView;
...
- (void)viewDidLoad
{
[super viewDidLoad];
[self arn_swizzRespondsToSelector:@selector(tableView:cellForRowAtIndexPath:) fromProtocol:@protocol(UITableViewDataSource) usingBlock:^UITableViewCell *(id obj, UITableView *tableView, NSIndexPath *indexPath) {
// Call!!
static NSString *cellIdentifier = @"cellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
cell.textLabel.text = @"Swizz!";
return cell;
}];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Not Call
return nil;
}
- (IBAction)tapSwizzButton:(id)sender
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"test" message:@"Swizz" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
[self arn_swizzRespondsToSelector:@selector(alertView:clickedButtonAtIndex:) fromProtocol:@protocol(UIAlertViewDelegate) usingBlock:^(id obj, UIAlertView *alertView, NSInteger buttonIndex) {
// Call!!
}];
[alertView show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
// Not Call
}
源代码在MIT许可证下分发。
这是最简单最宽容的许可证。
参考ReactiveCocoa的Message Forwarding机制,
创建了一个用于替换现有实例方法的类别。
实例方法的重写只对被重写的实例产生影响。
关于ReactiveCocoa的实现,以下解释为参考。
http://qiita.com/ikesyo/items/9c6b00e2b00d8f5e3e11
由于验证不够充分,不建议使用。