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

Weak, Strong, Unowned - Hector Matos

1fa9cb8c7997c8c4d3d251fb5e41f749?s=47 Realm
August 03, 2015
47k

Weak, Strong, Unowned - Hector Matos

1fa9cb8c7997c8c4d3d251fb5e41f749?s=128

Realm

August 03, 2015
Tweet

Transcript

  1. Weak, Strong, Unowned Oh, My!

  2. Quick Intro

  3. Hi! I'm Hector Matos

  4. None
  5. Sr. Software Engineer @ Capital One

  6. @allonsykraken on the Twitters

  7. Father of KrakenDev.io

  8. ARC

  9. - (void)releaseTheKraken { Kraken *kraken = [[Kraken alloc] init]; //+1

    retain [kraken release]; //generated by ARC }
  10. Magic has it's limitations And so does ARC

  11. Leaked Memory & Retain Cycles

  12. None
  13. None
  14. Strong References Default in Swift

  15. Now Stop, Example Time //Kraken holds a strong reference to

    the yummy human. class Kraken { var yummyHuman: Human }
  16. // The animation closure holds a strong reference to self

    // self.retainCount is 1 here UIView.animateWithDuration(0.3) { // self.retainCount is 2 here self.view.alpha = 0.0 }
  17. Questions?

  18. Weak References Safe - Independent

  19. Stop, Collaborate, & Listen! (to my next example) class KrakensFace:

    UIView { @IBOutlet weak var razorSharpTeeth: UIView! } KrakenAPI.eat(yummyHuman, whenFinished: { [weak self] in self?.waitForNextMealTime() })
  20. WTF is "[weak self] in"?!

  21. Capture Lists It's just an array. That's all it is.

    There, there. It's all over now. !
  22. KrakenAPI.eat(yummyHuman, finished: { [weak self, unowned lovedOne = yummyHuman.lovedOne] in

    self?.waitForNextMealTime() })
  23. Questions?

  24. Unowned References Mutual Dependence

  25. class Human { var heart: ! func seeKrakenComing() { heart.haveHeartAttack()

    } } class !: Organ { unowned let human: Human init(human: Human) { self.human = human } func haveHeartAttack() { human.die() } } let human = Human() human.heart = !(human: human)
  26. Questions?

  27. None