swift

14.Swift 闭包

2016-02-26  本文已影响1323人  Liwx

@(〓〓 iOS-Swift语法)[Swift 语法]


目录


闭包的介绍


闭包的使用


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
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.httpTool loadRequest:^{
        NSLog(@"主线程中,将数据回调.%@", [NSThread currentThread]);
    }];
}
block的写法:
    类型:
    返回值(^block的名称)(block的参数)

    值:
    ^(参数列表) {
        // 执行的代码
    };

使用闭包代替block

class WXNetworkTool: NSObject {

    // 注意: 如果写成var complete : (String) -> ()?, 不包含大括号,则编译器会解析成闭包的返回值为可选类型
    
    // 闭包的格式: (参数列表) -> (返回值类型)
    func loadData(complete : (String) -> ()) {
        
        dispatch_async(dispatch_get_global_queue(0, 0)) { () -> Void in
            
            print("\(NSThread.currentThread()): 正在请求数据")
            
            dispatch_sync(dispatch_get_main_queue(), { () -> Void in
                
                print("\(NSThread.currentThread()): 请求到数据,并且准备进行回调")
                
                // 回调闭包
                complete("JSON数据")
                
            })
            
        }
    }
    
}
// ------------------------------------------------------------------------
    // 重写父类的touchBegin方法,记得写override
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        
        // 闭包的调用
        network?.loadData({ (data : String) -> () in
            print("在touchBegin中获取数据")
        })
    }
闭包的写法:
    类型:(  形参列表)->(返回值)
    技巧:初学者定义闭包类型,直接写()->().再填充参数和返回值

    值:
    {
        (形参) -> 返回值类型 in
        // 执行代码
    }
// 如果闭包没有参数,没有返回值.in和in之前的内容可以省略.
    httpTool.loadRequest({
        print("回到主线程", NSThread.currentThread());
    })
// ----------------------------------------------------------------
        // 1.尾随闭包
        // 尾随闭包:如果一个闭包是方法的最后一个参数,那么方法后面的()可以直接跟在方法后
        network?.loadData() { (data : String) -> () in
            
            print("touchBegin")
        }
    // 开发中建议该写法,可以省略参数的括号()
        network?.loadData { (data : String) -> () in
            
            print("touchBegin")
        }

闭包的循环引用

// deinit析构函数,相当于OC的dealloc
    deinit {
        print("deinit 释放 ViewController")
    }
class WXNetworkTool: NSObject {

    // 注意: 如果写成var complete : (String) -> ()?, 不包含大括号,则编译器会解析成闭包的返回值为可选类型
    // 闭包是对象
    var complete : ((String) -> ())?
    
    // 闭包的格式: (参数列表) -> (返回值类型)
    func loadData(complete : (String) -> ()) {
        
        // 在闭包中,如果属性名与参数同名,属性需加上self.
        self.complete = complete
        
        dispatch_async(dispatch_get_global_queue(0, 0)) { () -> Void in
            
            print("\(NSThread.currentThread()): 正在请求数据")
            
            dispatch_sync(dispatch_get_main_queue(), { () -> Void in
                
                print("\(NSThread.currentThread()): 请求到数据,并且准备进行回调")
                
                // 回调闭包
                complete("JSON数据")
                
            })
            
        }
    }
    
}
        // ----------------------------------------------------------------
        // 2.解决闭包循环引用,解决方案一
        // 2.1 使用weak修饰,相当于OC中的__weak,当对象被释放是,weak修饰的对象指针会自动指向nil
//        weak var weakSelf : UIViewController? = self
        // 可以使用类型推导出self的类型
        weak var weakSelf = self
        network?.loadData({ (data : String) -> () in
            
            print("在touchBegin获取到数据\(data)")
            
              // 注意: 此时weakSelf的类型是可选类型,需要加上?
            weakSelf?.view.backgroundColor = UIColor.yellowColor()
            
        })
        // 2.2 在闭包的{之后添加[weak self] (推荐使用)
        network?.loadData({ [weak self] (data : String) -> () in
            
            print("在touchBegin获取到数据\(data)")
            // 此处self是可选类型
            self?.view.backgroundColor = UIColor.redColor()
        })
        // 2.3 在闭包的{之后添加[weak self],相当于OC的unsafe_unretained.当对象被释放时unowned修饰的对象指针还是指向原来的地址,不会自动指向nil.如果对象被释放了,之后又通过该指针去访问对象,则会出现坏内存访问.
        network?.loadData({ [unowned self](data : String) -> () in
            
            print("在touchBegin获取到数据\(data)")
            
            self.view.backgroundColor = UIColor.redColor()
            
        })
上一篇下一篇

猜你喜欢

热点阅读