OC高效率52之爲經常使用的塊類型建立typedef

#import <UIKit/UIKit.h>
/**
 *  typedef 帶「block」參數的方法
 */
typedef void (^EOCcompletion)(BOOL flag , int value);

@interface ViewController : UIViewController


/**
 *  用塊作參數的方法
 *
 */
-(void)startWithCompletionHandler:(void (^)(NSData *data,NSError *error))completion;
/**
 *  typedef 修飾過的帶block參數的方法
 */
-(void)startWithEOCCompletionHandler:(EOCcompletion)completion;

@end


#import "ViewController.h"
typedef int(^SomeblockName)(BOOL flag,int value);
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    /**
     * 每一個塊都具有其「固有類型」,由於可將其賦值給適當類型的變量
     *
     */
    int (^variableName)(BOOL flag, int a) = ^(BOOL flog,int value){
        if (flog){
            return  value *5;
        }else
        {
            return value *10;
        }
    };
    SomeblockName block = ^(BOOL flag, int value){
        return value *15;
    };
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
相關文章
相關標籤/搜索