定义闭包属性
var block02 = {
(str1:String,str2:String)in
//TODO:Do something
}
或
func myblock(str:String) -> Void {
}
var block03:(String)->Void = myblock
或直接构建
var block06:((String)->Void)!
let blvc = BlockVC()
blvc.block02 = {
(str:String,str2:String) in
print(str+str2)
}
blvc.block03 = {
(str:String) in
print(str)
}
blvc.block06({ (str) in
print(str)
})
闭包函数
func block01(str:String,block:(String) -> Void) ->Void {
print(str)
block("这是block")
}
调用
blvc.block01(str:"传值", block: { (str) in
print("读取block回调的东西\(str)")
})
逃逸闭包(@escaping)
/// 定义可以逃逸闭包的block的函数
/// - Parameters:
/// - str: 传入值
/// - block: block
func block04(str:String,block:@escaping(String)-> Void) -> Void {
print(str)
self.block05 = block//@escaping可以让block的指向跳出函数
}
调用
blvc.block04(str:"第四种block使用", block: { (str) in
print(str)
})
网友评论