微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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里子页面给父页面传值**

工程目录

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里子页面给父页面传值**

工程目录

swift-闭包.png


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

typealias changUserName = (String) ->()

把闭包申明成属性

var changText: changUserName?

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

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

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

    self.navigationController?.popViewControllerAnimated(true)
}

**在跳转按钮的方法里我们这样写(两种方法,对不两种不同属性哦)

ClickAction() {
    let secondVC = SecondViewController()
    // 防止循环引用
    weak var WeakSelf = self
    // 第一用方法
    secondVC.changText = { (names) -> () "------\(names)")
    WeakSelf!.userNames!.text = names
    }
        // 第二用方法
// secondVC.setMyChangeName { (names) in
// print("------\(names)")
// WeakSelf!.userNames!.text = names
// }
        self.navigationController?.pushViewController(secondVC,animated: true)
    }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐