Escaping Closures in Swift - 公司內分享會

481a04548eda340e5cf166b487792ee5?s=47 Li-Hsuan Chen
September 23, 2016

Escaping Closures in Swift - 公司內分享會

481a04548eda340e5cf166b487792ee5?s=128

Li-Hsuan Chen

September 23, 2016
Tweet

Transcript

  1. &TDBQJOH$MPTVSFT  ꤫խ愴蠩 @escaping

  2. 䖰ㆭ酭鷗鷗⿡ㆭ酭

  3. 殹DMPTVSF⡲捀⿮侸⫄Ⰵ♧⦐NFUIPE儘 鸏⦐DMPTVSF剚㖈NFUIPE㛂遤㸤䧭䖕 䩞剚鄄㛂遤儘㽠䗳갭䭸㹁䧭FTDBQJOHDMPTVSF

  4. ⾲㨥䞕㞯 雊䧮⦛⢵䢩䢩⸈匌銯

  5. class A { func someMethod(closure: () -> Void) { closure()

    } } 鸏♧媯♶剚剤DPNQJMFSFSSPS
  6. 䞕㞯♧ 鸏⦐DMBTT剤⦐荈㹁纏涸RVFVF ⢵嶋⻋♶ずNFUIPE⫄Ⰵ涸DMPTVSFT

  7. class A { var closures: [() -> Void]? = []

    func someMethod(closure: () -> Void) { self.closures?.append(closure) } } 㖈4XJGUⴀ⢵⟃⵹鸏媯♶剚剤㉏겗
  8. class A { var closures: [() -> Void]? = []

    func someMethod(closure: () -> Void) { self.closures?.append(closure) } }
  9. None
  10. class A { var closures: [() -> Void]? = []

    func someMethod(closure: @escaping () -> Void) { self.closures?.append(closure) } }
  11. 㼭穡 • @escaping ˖ ⫄ⰅNFUIPE涸DMPTVSF㥶卓銴㖈NFUIPE GVODUJPO涸SFUVSO䖕䩞㛂遤㽠䗳갭銴⸈♳ @escaping ˖ @noescapeJNQMJDJUMZ ˖

    DMPTVSF⡲捀⿮侸갸鏤鿪僽@noescape
  12. 䞕㞯✳ 䧮欽痧♲倰MJCSBSZ涸NFUIPE FH"MBNPSF

  13. 〫銴➮剤FTDBQJOH ⡹㽠銴騈衽!FTDBQJOH

  14. class B { var closures: [() -> Void]? = []

    func anotherMethod(closure: @escaping () -> Void) { self.closures?.append(closure) } }
  15. class A { func someMethod(closure: () -> Void) { B().anotherMethod(closure:

    closure) } } class B { var closures: [() -> Void]? = [] func anotherMethod(closure: @escaping () -> Void) { self.closures?.append(closure) } } 㖈4XJGUⴀ⢵⟃⵹鸏垺㻨♶剚剤㉏겗
  16. class A { func someMethod(closure: () -> Void) { B().anotherMethod(closure:

    closure) } } class B { var closures: [() -> Void]? = [] func anotherMethod(closure: @escaping () -> Void) { self.closures?.append(closure) } }
  17. None
  18. class A { func someMethod(closure: @escaping () -> Void) {

    B().anotherMethod(closure: closure) } } class B { var closures: [() -> Void]? = [] func anotherMethod(closure: @escaping () -> Void) { self.closures?.append(closure) } }
  19. 籏穡♧♴

  20. 䖰ㆭ酭鷗鷗⿡ㆭ酭

  21. 鷗ⴀ㛂遤涸眕㕠

  22. Ⱖ➮ ˖ 〳⟃鍑对SFUBJODZDMF涸㉏겗 4&  䖊㻜뀿  ˖ OPFTDBQFDMPTVSFTIBWFBMTPBMXBZTCFFOUIF QSFGFSSFEEFGBVMU

    TJODFUIFZFMJNJOBUFBDMBTT PGSFUBJODZDMFJTTVFT ˖ QSPUPDPM涸㹁纏 ˖ 㥶卓㻜⡲QSPUPDPM涸倰岁儘꨾銴⸈!FTDBQJOH  QSPUPDPM⚥鑪倰岁涸㹁纏⛳銴⸈♳
  23. 5IBOL:PV