通常在Swift中定义一个闭包来使用

typealias Closure= (Any?) -> ()

var tempClosure :Closure?

/// 定义一个方法直接调用
func closure(_ closure:@escaping closure) {
        self.closure = closure
}

 

 上面就是我们经常使用闭包的方法,这个方法的弊端就是,在项目工程中typealias满天飞,到处都是定义的闭包,这个时候结合泛型对闭包做如下的修改

public typealias GenericClosure<T> = (T) -> ()

 

这样定义之后我们在需要使用的时候就可以随意的定义闭包的参数类型了

var stringClosure:GenericClosure<String>?
var stringClosure:GenericClosure<(title:String,model:Any)>?
var voidClosure:GenericClosure<()?>

 

相关文章:

  • 2021-11-29
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-08
  • 2022-12-23
猜你喜欢
  • 2021-08-12
  • 2022-12-23
  • 2022-12-23
  • 2021-06-26
  • 2022-12-23
  • 2021-07-23
  • 2021-11-29
相关资源
相似解决方案