OC的block和Swift的闭包写法

Object-c的block和Swift的闭包的对比

本来来说说在OC和Swift的block和闭包的区别和使用
主要以对比的形式写出。

gitHub:https://github.com/7General/OC-BlocK-Swift/tree/master

Object-c申明一个在函数中使用Block且不带参数和没返回值的block

// ****1:在函数中使用Block不带参数和没返回值的block
-(void)AFNetWork:(NSString *)name withComplated:(void(^)())complated {
    NSLog(@"----函数中打印-%@",name);
    if (complated) {
        complated();
    }
}

// ********调用函数
[self AFNetWork:@"ZZ" withComplated:^{
   NSLog(@"----Block--打印");
}];

Swift 声明不带参数和没返回值的闭包

// 声明不带参数和没返回值的闭包
func HttpTools(names: Int ,complated:() -> ()) -> Int {
    let resInt = names + 10
    print("1:先执行函数")
    complated()
    return resInt
}

// ********调用
HttpTools(15) { 
    print("2:在执行Block了")
}
Object-C 声明带参数和有返回值的Block在函数名中
// ***2:声明带参数和有返回值的Block在函数名中
-(void)AFNetWork:(NSString *)name withComplatedRetunStr:(NSString *(^)(NSString * names,NSString * school))complated {
    NSLog(@"----函数中打印带参数有返回值-%@",name);
    if (complated) {
        complated(name,@"军事博物馆");
    }
}

// ********调用
[self AFNetWork:@"中国" withComplatedRetunStr:^NSString *(NSString *names,NSString * school) {
    NSLog(@"----Block函数中打印带参数有返回值-%@-----%@",names,school);
    return names;
}];
Swift 声明带参数和有返回值的闭包在函数名中
// 声明带参数和有返回值的闭包在函数名中
func ajaxTools(name:String ,complated:(runStr: String,isStop:Bool) -> String) -> String {
    let resStr = name + "覆水难收"
    complated(runStr: resStr, isStop: true)
    return resStr + " - 内部函数返回"
}

// ********调用
let ajaxResult = ajaxTools("洲洲哥") { (runStr, isStop) -> String in
    print("-----\(runStr)")
    return ""
}
**Object-C里子页面给父页面传值**

工程目录

1240

OC-Block.png

在SecondViewController.h文件中 申明一个Block

typedef void(^changUserName)(NSString * userNames);

把Block申明成属性

@property (nonatomic, copy) changUserName  changText;
// 还可把set方法抛出来(或者使用实例方法调用)
-(void)setChangText:(changUserName)changText;

点击返回按钮的回调方法我们要这样写

-(void)playVideoBack {
    if (self.changText) {
        self.changText(self.inputFiled.text);
    }
    [self.navigationController popViewControllerAnimated:YES];
}
**在跳转按钮的方法里我们这样写(两种方法,对不两种不同属性哦)
-(void)ButtonClick {
    SecondViewController * sec = [[SecondViewController alloc] init];
    /**防止循环引用*/
    __weak typeof(self) WeakSelf = self;
// 第一种写法
//    sec.changText = ^(NSString * textStr) {
//        WeakSelf.userNames.text = textStr;
//        [WeakSelf AFNetWork:@"历史遗留痕迹" withComplated:^{
//            NSLog(@"----block---弱引用");
//        }];
//    };
// 第二种写法
    [sec setChangText:^(NSString *userNames) {
        WeakSelf.userNames.text = userNames;
                [WeakSelf AFNetWork:@"历史遗留痕迹" withComplated:^{
                    NSLog(@"----block---弱引用");
                }];
    }];

    [self.navigationController pushViewController:sec animated:YES];
}
**Swift里子页面给父页面传值**

工程目录

1240

swift-闭包.png


在SecondViewController.swift文件中 申明一个闭包

typealias changUserName = (String) ->()

把闭包申明成属性

var changText: changUserName?

// 或者使用实例方法调用(方法名字不固定,但参数是必须的)
func setMyChangeName(tempClose: changUserName)  {
    self.changText = tempClose
}

点击返回按钮的回调方法我们要这样写

func pushClick()  {
    changText!(self.changName.text!)

    self.navigationController?.popViewControllerAnimated(true)
}
**在跳转按钮的方法里我们这样写(两种方法,对不两种不同属性哦)
 func ClickAction() {
    let secondVC = SecondViewController()
    // 防止循环引用
    weak var WeakSelf = self
    // 第一用方法
    secondVC.changText = { (names) -> () in
    print("------\(names)")
    WeakSelf!.userNames!.text = names
    }
        // 第二用方法
// secondVC.setMyChangeName { (names) in
// print("------\(names)")
// WeakSelf!.userNames!.text = names
// }
        self.navigationController?.pushViewController(secondVC, animated: true)
    }

转载于:https://my.oschina.net/iceTear/blog/1586158

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值