OC中RAC编程block的基本使用

在OC中block的基本使用

//
//  ViewController.h
//  RAC——test
//
//  Created by Aaron on 17/1/17.
//  Copyright © 2017年 Aaron. All rights reserved.
//

#import <UIKit/UIKit.h>

//对于block的全局声明。
typedef void(^myBlock)(NSString *str);

@interface ViewController : UIViewController
//全局声明的使用写法
//- (void)makeTextwithBlock:(myBlock)block;
//直接写block写法
- (void)makeTextwithBlock:(void(^)(NSString *str))block;
@end
//
//  ViewController.m
//  RAC——test
//
//  Created by Aaron on 17/1/17.
//  Copyright © 2017年 Aaron. All rights reserved.
//

#import "ViewController.h"
@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    //调用获取到拿到的值
    [self makeTextwithBlock:^(NSString *str) {
        NSLog(@"%@",str);
    }];
    //block的声明赋值
    NSString * (^myblock1)(NSString *) = ^(NSString *str){
        return str;
    };
   //block的调用
    NSString *ss = myblock1(@"你好aaa");
    NSLog(@"%@",ss);
}
//实现传值
- (void)makeTextwithBlock:(void (^)(NSString *))block
{
    block(@"你好");
}

@end
posted @ 2017-01-17 15:03  懒懒初阳  阅读(251)  评论(0编辑  收藏  举报