Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Upcoming SlideShare
PARTHA PRATIM ESTs
Next
Download to read offline and view in fullscreen.

0

Share

Download to read offline

Swift study: Closure

Download to read offline

Swift, Closure

Related Books

Free with a 30 day trial from Scribd

See all

Related Audiobooks

Free with a 30 day trial from Scribd

See all
  • Be the first to like this

Swift study: Closure

  1. 1. Closure
  2. 2. First Class Function func toStr(x: Int) -> String { return "(x)" } toStr(3) // functions can be assigned to a variable. let toStr2 = { (x: Int) -> String in return "(x)" } toStr2(4) Syntax Sugar
  3. 3. Pass a function as a parameter let toStr = { (x: Int) -> String in return "(x)" } let toStr2 = { (x: Int) -> String in return "a(x)" } toStr(5) toStr2(5) func ajax(callback: (x: Int) -> String) { callback(x: 10) } ajax(toStr) // 10 ajax(toStr2) // a10 type of the function ((x:Int) -> String)
  4. 4. lambda calculas ajax({(param: Int) -> String in return “(param)” }) ajax({(param: Int) -> String in return “($0)” }) ajax({(param: Int) -> String in return “($0)” }) ajax(){“($0)”} Complete Short Hand
  5. 5. Closure var result = 0 // caller’s result ajax({ result++ }) func ajax(callback: ()->()) { var result = 10 // nothing to do with the caller’s result callback() callback() } println(result) // 2, not 12
  6. 6. Callback var result: String ajax( { result = $0 }, fail:{ result = “NG” } ) func ajax(ok: (x:String)->(), fail fail:()->()) { if let ret = get_string_via_ajax() { ok(x:ret) } else { fail() } }
  7. 7. map (transformer) let list = [1, 2, 3] list.map({$0 x 3}) // [3, 6, 9] list.map(){$0 x 3} list.map{$0 x 3}

Swift, Closure

Views

Total views

481

On Slideshare

0

From embeds

0

Number of embeds

11

Actions

Downloads

5

Shares

0

Comments

0

Likes

0

×