Upgrade to Pro — share decks privately, control downloads, hide ads and more …

weak-vs-unowned

Yutaro Muta
September 02, 2018

 weak-vs-unowned

Yutaro Muta

September 02, 2018
Tweet

More Decks by Yutaro Muta

Other Decks in Technology

Transcript

  1. • Yutaro Muta @yutailang0119 • Hatena Co., Ltd. @Kyoto •

    Conference Staff • builderscon 2017, 2018 • PyCon JP 2016, 2017 • and more • Who am I ?
  2. pOBMDMBTT1BSFOU7JFX$POUSPMMFS6*7JFX$POUSPMMFS\ QSJWBUFGVODBEE$IJME7JFX CZSFGFSFODF5ZQF3FGFSFODF5ZQF \ MFUTUPSZCPBSE6*4UPSZCPBSE OBNF$POUBJOFS4BNQMF CVOEMFOJM MFUDIJME7JFX$POUSPMMFSTUPSZCPBSEJOTUBOUJBUF7JFX$POUSPMMFS XJUI*EFOUJpFS$IJME BT$IJME7JFX$POUSPMMFS

    MFUDMPTVSF 4USJOH 7PJE TXJUDISFGFSFODF5ZQF\ DBTFTUSPOH DMPTVSF\UFYUJO TFMGUFYU-BCFMUFYUUFYU ^ DBTFXFBL DMPTVSF\<XFBLTFMG>UFYUJO TFMG UFYU-BCFMUFYUUFYU ^ DBTFVOPXOFE DMPTVSF\<VOPXOFETFMG>UFYUJO TFMGUFYU-BCFMUFYUUFYU ^ ^ DIJME7JFX$POUSPMMFSSFGFSFODF5ZQFSFGFSFODF5ZQF DIJME7JFX$POUSPMMFSUBQ$MPTVSFDMPTVSF BEE$IJME7JFX$POUSPMMFS DIJME7JFX$POUSPMMFS DIJME7JFX$POUSPMMFSWJFXGSBNFDPOUBJOFS7JFXCPVOET DPOUBJOFS7JFXBEE4VCWJFX DIJME7JFX$POUSPMMFSWJFX DIJME7JFX$POUSPMMFSEJE.PWF UP1BSFOU7JFX$POUSPMMFSTFMG ^ ^ ॥؀ࢀরͷ෮श
  3. ༨ஊ: weakΩϟϓνϟ͸ self Ҏ֎΋Ͱ͖Δ pOBMDMBTT7JFX$POUSPMMFS6*7JFX$POUSPMMFS\ !*#0VUMFUXFBLWBSVSM-BCFM6*-BCFM !*#0VUMFUXFBLWBSRVFSZ*UFNT-BCFM6*-BCFM !*#"DUJPOGVODUBQ'PP @TFOEFS6*#VUUPO \

    SFRVFTU CZXFBL \<XFBLVSM-BCFM XFBLRVFSZ*UFNT-BCFM>SFTQPOTFJO MFUQBSTFE3FTQPOTF3FTQPOTF SFTQPOTFSFTQPOTF %JTQBUDI2VFVFNBJOBTZOD\ VSM-BCFM UFYUQBSTFE3FTQPOTF VSM RVFSZ*UFNT-BCFM UFYUQBSTFE3FTQPOTF BSHT ^ ^ ^ ^ ※ͨͩ͠ɺunowned͸Ͱ͖ͳ͍ `property` may only be applied to class and class-bound protocol types, not ‘Type'
  4. pOBMDMBTT$SBTI7JFX$POUSPMMFS6*7JFX$POUSPMMFS\ !*#"DUJPOGVODUBQ8FBL @TFOEFS6*#VUUPO \ SFRVFTU CZXFBL \<XFBLTFMG>SFTQPOTFJO %JTQBUDI2VFVFNBJOBTZOD"GUFS EFBEMJOF%JTQBUDI5JNFOPX 

     \ TFMG SFMPBE7JFX CZSFTQPOTF ^ ^ OBWJHBUJPO$POUSPMMFS QPQ7JFX$POUSPMMFS BOJNBUFEUSVF ^ !*#"DUJPOGVODUBQ6OPXOFE @TFOEFS6*#VUUPO \ SFRVFTU CZVOPXOFE \<VOPXOFETFMG>SFTQPOTFJO %JTQBUDI2VFVFNBJOBTZOD"GUFS EFBEMJOF%JTQBUDI5JNFOPX   \ TFMGSFMPBE7JFX CZSFTQPOTF ^ ^ OBWJHBUJPO$POUSPMMFS QPQ7JFX$POUSPMMFS BOJNBUFEUSVF ^ ^ ϝϦοτ: UnownedͰΫϥογϡ͢Δঢ়گ΋ճආͰ͖Δ
  5. σϝϦοτ: ৑௕ʹͳΔ͜ͱ͕͋Δ pOBMDMBTT7JFX$POUSPMMFS6*7JFX$POUSPMMFS\ QSJWBUFMFUOBNFZVUBJMBOH !*#"DUJPOGVODUBQ'PP @TFOEFS6*#VUUPO \ SFRVFTU CZXFBL \<XFBLTFMG>@JO

    HVBSEMFUTUSPOH4FMGTFMGFMTF\ SFUVSO ^ QSJOU TUSPOH4FMGOBNF ^ ^ ^ if let ΍ guard let Ͱ৑௕ʹΞΫηε੍ޚ͢Δඞཁ͕ग़ͨΓ͢Δ ಛʹ self ͸Shadowingͷ໰୊͕͋Δ
  6. Stack Overflow weak vs unowned in Swift. What are the

    internal differences? https://stackoverflow.com/a/42847825
  7. Stack Overflow weak vs unowned in Swift. What are the

    internal differences? https://stackoverflow.com/a/42847825 > In my usage, the common case arises in situations where a closure needs a capture list involving self in order to avoid a retain cycle. In such a situation, it is almost always possible to say [unowned self] in the capture list. When we do: > * It is more convenient for the programmer because there is nothing to unwrap. [weak self] would be an Optional in need of unwrapping in order to use it. > * It is more efficient, partly for the same reason (unwrapping always adds an extra level of indirection) and partly because it is one fewer weak reference for the runtime's scratchpad list to keep track of.
  8. Stack Overflow weak vs unowned in Swift. What are the

    internal differences? https://stackoverflow.com/a/42847825 > In my usage, the common case arises in situations where a closure needs a capture list involving self in order to avoid a retain cycle. In such a situation, it is almost always possible to say [unowned self] in the capture list. When we do: > * It is more convenient for the programmer because there is nothing to unwrap. [weak self] would be an Optional in need of unwrapping in order to use it. > * It is more efficient, partly for the same reason (unwrapping always adds an extra level of indirection) and partly because it is one fewer weak reference for the runtime's scratchpad list to keep track of.
  9. The Swift Programing Language Automatic Reference Counting / Unowned References

    > Unlike a weak reference, however, an unowned reference is used when the other instance has the same lifetime or a longer lifetime. You indicate an unowned reference by placing the unowned keyword before a property or variable declaration. > An unowned reference is expected to always have a value. As a result, ARC never sets an unowned reference’s value to nil, which means that unowned references are defined using nonoptional types. https://docs.swift.org/swift-book/LanguageGuide/AutomaticReferenceCounting.html
  10. The Swift Programing Language Automatic Reference Counting / Unowned References

    > Unlike a weak reference, however, an unowned reference is used when the other instance has the same lifetime or a longer lifetime. You indicate an unowned reference by placing the unowned keyword before a property or variable declaration. > An unowned reference is expected to always have a value. As a result, ARC never sets an unowned reference’s value to nil, which means that unowned references are defined using nonoptional types. https://docs.swift.org/swift-book/LanguageGuide/AutomaticReferenceCounting.html
  11. Reference • https://qiita.com/susieyy/items/39efd4d34cf4c1f9d3ef • https://docs.swift.org/swift-book/LanguageGuide/AutomaticReferenceCounting.html • https://github.com/yutailang0119/iOSDC2018Sample • https://help.apple.com/instruments/mac/current/ •

    https://stackoverflow.com/questions/42842336/weak-vs-unowned-in-swift-what-are- the-internal-differences • http://www.apeth.com/iOSBook/ch12.html#_property_memory_management_policies