Day145 — path to iOS developer

Jacky Tsang
2 min readMar 2, 2021

--

Section 21

Computed Properties

if only get is used, that variable can only be got.

Fairly usable if multiple variables are related to each other. No function for maintaining that relationship is needed. It makes the code easier to understand.

Observed Properties

Access Level

private < fileprivate < internal (default) < public < open

Struct vs Class

Structs

  • value type (store the actual data values. if copied, a new one is created.)
  • lives on the stack, FILO
  • have a default initializer
  • Deep copies
  • true immutability (Once a value inside a Struct is changed, a new struct is created with the changed value.)
  • no memory leaks
  • thread-safe

Classes

  • reference type (store a reference to a block of memory (in the heap). if copied, only a new reference pointing to the same memory is created.)
  • lives in the heap (when a class is initialized, all of that data gets allocated some memory and it will be saved in the heap. Reference of that class instance lives on the stack, and point towards the allocated block of data in the heap)
  • has inheritance
  • works with objective-C code

Summary: start with Structs, use Classes when inheritance is needed.

Tuple

guard let vs if let

guard let = guarding against something terrible. don’t expect the else case (value being nil) to be triggered

if let = expect nil value to occur sometime

--

--

No responses yet