简体   繁体   中英

how to use different closure types in swift

I am studying the clussures in swift but i dont really get the difference between these function types and what is the point of these defferences .I do understand the first and the second one somehow but the last one makes me commpletely confused . for example :

 func performMagic (thingy : String ){ 
      return thingy 
    }
performMagic("hello")

&

var newMagicFunction ={
  (thingy : String) -> String in 
return thingy
}

&

var addeFunction : (Int , Int) -> Int ={
  (a : Int , b : Int) -> Int in 
return a + b
} 
addeFunction(1,3)

Thanks

the only real difference between the last one and the 2nd last one, is that the type of the var is being declared in the last one, and the type of the var is inferred from what is being assigned to it in the 2nd one. (besides the obvious functionality difference) eg, you could swap them around sort of

var newMagicFunction : String -> String = {
  (thingy : String) -> String in 
  return thingy
}


var addeFunction = {
  (a : Int , b : Int) -> Int in 
  return a + b
} 
addeFunction(1,3)

which are equivalent to the above ones you posted

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM