闭包有多种类型,我们可以使用带有 void return 、单参数返回和多返回类型的闭包,
我们可以用inout , typealaise , escaping , autoclosure
关键词以及闭包中的功能。
实际上,闭包是带有参数和返回类型的自包含块
这里我添加了闭包代码
类 ClouserVC : UIViewController{
var clouserPrint = {
print("Clouser just print value")
}
var closureInoutData = { ( value : inout String) in
value = "Value Changed"
}
typealias colsureTypealaise = ((String) -> Void)
var valueforInout = "initail value"
var valueforClosureInout = "Clouser Value"
override func viewDidLoad() {
super.viewDidLoad()
self.clouserPrint()
self.closureWithFun {
print("==>This from closureWithFun , but it pass empty data")
}
print("=!=> Value before inout Closure" , valueforInout)
self.closureInoutData(&valueforInout)
print("=!=> Value after inout Closure" , valueforInout)
self.closureWithValidFun { (argument) -> String in
print("===> getting passed value from closure \(argument)")
return ""
}
self.closureWithTypeAlise { (typeAlasie) in
print("====> This value declared in Typealasie , \(typeAlasie)")
}
self.closureWithMultipleParams { (strValue, intValuse) -> (String) in
print("=====> get mupltiple datas from function , \(strValue) , \(intValuse.description)")
return strValue
}
let closureReturns = self.funcWithClouserReturn(argument: 10)
self.funcwithEscapingClosure { (escaping) in
print("=======> get escaping closure values === \(escaping)")
}
print("=8=> Value before inout FunClosure" , valueforClosureInout)
self.funcWithClosureInOutParam { (inoutData) in
print("=*8=>",inoutData )
inoutData = "valueChangefor InoutClosure"
}
print("=8=> Value after inout FunClosure ===" , valueforClosureInout)
}
func closureWithFun(argument : ()-> Void){
print("==>Function with argument closure , but it passes empty data")
}
func closureWithValidFun(argument : (String) -> String){
print("===> Function have valid argument")
argument("value_passed_from_by_this_type")
}
func closureWithTypeAlise(argumenttype :colsureTypealaise){
print("====> Function have Typealaised params")
argumenttype("Hello Alaise")
}
func closureWithMultipleParams(multiple params : (String,Int) -> (String)){
print("=====> Here we pass multiple params to returnback to its function call" )
params("Multiple",2)
}
func funcWithClouserReturn(argument params : Int) -> () ->(String){
print("======> Closure returns from the function")
func closuserInfunc() -> String{
return "closure value inside the func \(params)"
}
return closuserInfunc
}
func funcwithEscapingClosure(argument Param: @escaping(String)->()){
print("=======> This func have Excaping closure as a param")
Param("Hello")
}
func funcWithClosureInOutParam(argument param : (inout String) -> ()){
print("=8=> This func have inout argument")
param(&valueforClosureInout)
}
}