ios block使用

BlockTest.h

#import <Foundation/Foundation.h>

typedef void (^didFinishBlock)(NSString *str);
typedef void (^didFailBlock)(NSString *str);

@interface BlockTest : NSObject

+(void)justForTest:(NSString *)string finishBlock:(didFinishBlock)finishBlock failBlock:(didFailBlock)failBlock;

@end

BlockTest.m

#import "BlockTest.h"

@implementation BlockTest

+(void)justForTest:(NSString *)string finishBlock:(didFinishBlock)finishBlock failBlock:(didFailBlock)failBlock
{
    if ([string isEqualToString:@"test"]) {
         finishBlock(string);
    }
    else
    {
        failBlock(@"参数错误");
    }
   
}

@end

// 使用

 [BlockTest justForTest:@"testt" finishBlock:^(NSString *str) {
       NSLog(@"%@",str);
   } failBlock:^(NSString *str) {
       NSLog(@"%@",str);
   }];

 

posted @ 2015-02-03 17:07  菜鸟程序猿  阅读(194)  评论(0编辑  收藏  举报