Skip to content

Instantly share code, notes, and snippets.

@JesusMartinAlonso
Last active November 20, 2019 09:57
Show Gist options
  • Save JesusMartinAlonso/01ded62c3b0405ebe129ca06cb389738 to your computer and use it in GitHub Desktop.
Save JesusMartinAlonso/01ded62c3b0405ebe129ca06cb389738 to your computer and use it in GitHub Desktop.
Swift-iOS Cheatsheet

How to detect when app goes to background/foreground

override func viewWillAppear(_ animated: Bool) {
  NotificationCenter.default.addObserver(self, selector: #selector(didBecomeActive(notification:)),
                                                  name: UIApplication.didBecomeActiveNotification, object: nil)

  NotificationCenter.default.addObserver(self, selector: #selector(willResign(notification:)),
                                                  name: UIApplication.willResignActiveNotification, object: nil)
}

override func viewWillDisappear(_ animated: Bool) {
  NotificationCenter.default.removeObserver(self, name: UIApplication.didBecomeActiveNotification, object: nil)
  NotificationCenter.default.removeObserver(self, name: UIApplication.willResignActiveNotification, object: nil)
}

ViewController lifecycle

  • loadView
  • loadViewIfNeeded
  • viewDidLoad
  • viewWillAppear
  • viewWillLayoutSubviews
  • viewDidLayoutSubviews
  • viewDidAppear
  • viewWillDisappear
  • viewDidDisappear

Difference between frame and bounds

The bounds of an UIView is the rectangle, expressed as a location (x,y) and size (width,height) relative to its own coordinate system (0,0).

The frame of an UIView is the rectangle, expressed as a location (x,y) and size (width,height) relative to the superview it is contained within.

How to avoid memory leaks

We can define a memory leak as a portion of memory that is occupied forever and never used again. It is garbage that takes space and causes problems.

In iOS the number one reason of memory leaks is retain cycles. A retain cycle is created when two objects have a strong reference to the other object.

The solution is to use unowned or weak modifier in one of the references.

https://medium.com/flawless-app-stories/memory-leaks-in-swift-bfd5f95f3a74

Difference between unowned and weak

Both of them are weak references. The main diference is that weak is optional while unowned isn't. In this way, an unowned reference is always suppose to have a value. Otherwise, the app will crash.

Use unowned when you know that the life of object that holds this reference will be always shorter than the referenced object. Example: Credit card holding a reference to an Account. Credit card life will be always shorter than the account.

Otherwise, use weak

https://stackoverflow.com/questions/24011575/what-is-the-difference-between-a-weak-reference-and-an-unowned-reference

Value and reference types

iOS Architectures

https://medium.com/ios-os-x-development/ios-architecture-patterns-ecba4c38de52

  • MVC
  • MVP
  • MVVM
  • VIPER

Testing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment