ios - Closures In Swift? -


i new ios coding , stuck in closures feature of swift. have referred many tutorials , found closures self written codes can used in many ways eg. arguments in function call,parameters in function definition,variables. giving below example below associated thoughts code & questions. please me if wrong in understanding. know wrong @ many points,so please rectify me.

1.1st part

 func test(text1:string,text2:string,flag: (s1:string,s2:string)->bool)//in line,i think,i using flag closure passed parameter in function. , if why doesn't follow standard closure syntax?     {         if flag(s1: text1, s2: text2) == true//i want check return type flag closure gets when compares both string during function call. why can't write if flag == true flag name of closure , refers return type of closure?         {             print("they equal")         }         else         {           //         }     } 

2nd part

this part troublesome part confuses me when calling function. here using same closure. happening on here? how closure being used? capturing values or else?

test("heyy", text2: "heyy") { (s1, s2) -> bool in     s1==s2 } 

thanks kind consideration.

your first function works :

arguments :

  • string 1
  • string 2
  • a function takes 2 strings arguments , returns bool

body :

execute function (flag) text1 , text2 , check result. function doesn't know @ testing, knows 2 pieces of text needed , bool returned.


so function allows create general way of handling different functions have 2 strings input. can check equality or if first pieces of text part of second , on.

this useful many things , not far how array filtering / sorting / map works.


2nd part :

this how call function closure.

test("heyy", text2: "heyy") { (s1, s2) -> bool in     s1 == s2 } 

you can call :

func teststringequalityfor(text:string, and:string) -> bool {      return text == ,  }  test("hey", text2: "hey", flag: teststringequalityfor) 

instead of using trailing closure syntax pass unnamed function, pass named function 1 of arguments.


it al becomes lot clearer when simplify it.

this function takes function argument. can call/use function inside it. argument function takes bool it's argument. give true

func simplefunctionwithclosure(closure:(success:bool) -> void) {      // use closure     closure(success: true)  } 

when use function need pass function. in swift have trailing closure syntax that, available (and optional) first function argument.

trailing closure syntax means instead of passing named function can write:

myfunction { arguments-for-closure-as-tuple -> return-for-closure-as-tuple in      function-body  } 

the closure receive argument of bool , returns nothing void.

in body can handle arguments , stuff them. important remember inside closure not called directly. function declaration executed simplefunctionwithclosure

// use function simplefunctionwithclosure { (success) -> void in     if success {         print("yeah")     } else {         print("ow")     } } 

or named function :

func argumentfunction(success:bool) -> void {     if success {         print("yeah")     } else {         print("ow")     } }  simplefunctionwithclosure(argumentfunction) 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -