Swift闭包

闭包的介绍

闭包和OC中的block非常相似
OC中的block是匿名的函数
Swift中的闭包是一个特殊的函数
block和闭包都经常用于回调
闭包的使用

block的用法回顾

定义网络请求的类
@interface HttpTool : NSObject
- (void)loadRequest:(void (^)())callBackBlock;
@end

@implementation HttpTool
- (void)loadRequest:(void (^)())callBackBlock
{
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        NSLog(@"加载网络数据:%@", [NSThread currentThread]);

        dispatch_async(dispatch_get_main_queue(), ^{
            callBackBlock();
        });
    });
}
@end
进行网络请求,请求到数据后利用block进行回调
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.httpTool loadRequest:^{
        NSLog(@"主线程中,将数据回调.%@", [NSThread currentThread]);
    }];
}
block写法总结:
block的写法:
    类型:
    返回值(^block的名称)(block的参数)

    值:
    ^(参数列表) {
        // 执行的代码
    };
使用闭包代替block

定义网络请求的类
class HttpTool: NSObject {

    func loadRequest(callBack : ()->()){
        dispatch_async(dispatch_get_global_queue(0, 0)) { () -> Void in
            print("加载数据", [NSThread.currentThread()])

             dispatch_async(dispatch_get_main_queue(), { () -> Void in
                callBack()
             })
        }
    }
}
进行网络请求,请求到数据后利用闭包进行回调
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        // 网络请求
        httpTool.loadRequest ({ () -> () in
            print("回到主线程", NSThread.currentThread());
        })
    }
闭包写法总结:
闭包的写法:
    类型:(形参列表)->(返回值)
    技巧:初学者定义闭包类型,直接写()->().再填充参数和返回值

    值:
    {
        (形参) -> 返回值类型 in
        // 执行代码
    }
闭包的简写

如果闭包没有参数,没有返回值.in和in之前的内容可以省略
    httpTool.loadRequest({
        print("回到主线程", NSThread.currentThread());
    })
尾随闭包写法:
如果闭包是函数的最后一个参数,则可以将闭包写早()后面
如果函数只有一个参数,并且这个参数是闭包,那么()可以不写
    httpTool.loadRequest() {
        print("回到主线程", NSThread.currentThread());
    }
    // 开发中建议该写法
    httpTool.loadRequest {
        print("回到主线程", NSThread.currentThread());
    }
闭包的循环引用

如果在HttpTool中有对闭包进行强引用,则会形成循环引用
class HttpTool: NSObject {

    // 定义属性,来强引用传入的闭包
    var callBack : (()->())?

    func loadRequest(callBack : ()->()){
        dispatch_async(dispatch_get_global_queue(0, 0)) { () -> Void in
            print("加载数据", [NSThread.currentThread()])

             dispatch_async(dispatch_get_main_queue(), { () -> Void in
                callBack()
             })
        }

        self.callBack = callBack
    }
}
swift中解决循环引用的方式
    // weak var weakSelf = self;
    // [weak self] () -> () in
    // [unowned self] () -> () in
    httpTool.loadRequest { [unowned self] () -> () in
        self.view.backgroundColor = UIColor.redColor()
        print("回到主线程", NSThread.currentThread());
    }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容