#import <UIKit/UIKit.h>
typedef NSString*(^MyBlock) (NSString*,NSInteger);
@interface ViewController : UIViewController
-(MyBlock)GetDefaltBlock:(NSString*)Astr withInt:(NSInteger) Aint;
@end
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
MyBlock _b = [self GetDefaltBlock:@"Hello" withInt:123];
NSLog(@"%@",_b(@"World",456));
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
-(MyBlock)GetDefaltBlock:(NSString *)Astr withInt:(NSInteger)Aint
{
MyBlock b = ^NSString*(NSString* Bstr, NSInteger Bint)
{
return [NSString stringWithFormat:@"STR IS %@,INT IS %ld",
[Astr stringByAppendingString:Bstr],Aint + Bint];
};
return b;
}
@end
这个居然也能行,不可思议啊,一直以为访问不到astr运行的时候会有错误。
arc环境下,还挺神奇。
非arc环境参考http://bbs.9ria.com/thread-259515-1-1.html