DEV Community

Preslav Rachev
Preslav Rachev

Posted on

Properties in Swift: How to Avoid Shooting Yourself in the Foot

Note: This post was originally published on my blog.

Swift provides several constructs which make writing code a more fluid experience, with less boilerplate. Sometimes this succinctness of syntax comes at a potential cost though. One such aspect are properties, and more specifically, property initialisation. A small difference in the syntax might result in unnecessary memory consumption, unexpected state inconsistencies, etc. Those might remain unnoticed when the project is still small and reappear at a later stage, when the project is large enough to make it difficult to debug.

Though an experienced Swift programmer will immediately notice the difference between the following two expressions, people relatively new to the language may not. Consider the following expression:

var urlSession: URLSession = {
  let urlSessionConfiguration: URLSessionConfiguration = URLSessionConfiguration.default.copy() as! URLSessionConfiguration
  urlSessionConfiguration.requestCachePolicy = NSURLRequest.CachePolicy.returnCacheDataElseLoad

  // some further configuration

  return URLSession(configuration: urlSessionConfiguration)
}()
Enter fullscreen mode Exit fullscreen mode

and compare it with this one:

var urlSession: URLSession {
  let urlSessionConfiguration: URLSessionConfiguration = URLSessionConfiguration.default.copy() as! URLSessionConfiguration
  urlSessionConfiguration.requestCachePolicy = NSURLRequest.CachePolicy.returnCacheDataElseLoad

  // some further configuration

  return URLSession(configuration: urlSessionConfiguration)
}
Enter fullscreen mode Exit fullscreen mode

Noticed the difference? In case you haven't, the fllowing piece of code might help:

for _ in 1...10 {
  print(urlSession)
}
Enter fullscreen mode Exit fullscreen mode

While the former expression will always print out the same instance ID, the latter will output 10 different instances. Swift makes a distinction between stored properties with a closure initiializer (ex 1) and computed properties (ex 2). Stored properties get initialized once,and though their values may change over the lifetimeof the application, the initializer gets called only once. This is made clear by the fact that this is actually an assignment operation (denoted by the = operator) and that the closure gets executed prior to the assignment (The () brackets after the closure). A readonly computed property on the other hand is nothing more than a partial application of the full computed property declaration:

var myProp: MyClass {
  get {
    // optionally do some necessary pre-work
    return MyClass(/* set some intial values */)
  }
  set(newProp) {
    a = newProp.a
    b = newProp.b
  }
}
Enter fullscreen mode Exit fullscreen mode

A readonly computed property is one that has no setter, in which case, the get and set keyword can be omitted. Which leads us to the second example above. In its case, the closure, serving the purpose of a getter method, will be called every time the urlSession property is accessed. This in turn will create a new URLSession instance every time, potentially leading to an app slowdown, inconsistent state, or memory leaks.

NOTE: Since a computed property gets reassigned upon every access, it can only be declared as a var and not a let. The first of the examples however, is a stored property which is assigned only once, so might as well declare it as a let constant in case we won't need to reassign it again (always recommended)

Further Reading

Top comments (3)

Collapse
 
roddi profile image
Ruotger Deecke

In my opinion you should avoid computed properties whenever possible. It is exactly for the reasons that you lay out. If you see code like

let session = manager.urlSession

Would expect to get a brand new one session? No you wouldn't. That's clearly an unexpected side-effect. You would expect to always get the same.

Computed properties are evil exactly for those side-effects. But Juniors like to play with these shiny toys. …sigh…

So my rule of thumb is: If it has a side-effect make it a func with a name that clearly indicates said side-effect.

Collapse
 
eonist profile image
Eon • Edited

I store all my constants as static let in enum hierarchies. Lazy var that point to a computed prop for all else. Great article ✌️

Collapse
 
dimpiax profile image
Dmytro Pylypenko

Hello Preslav,

better to use lazy in using case of class or struct:

lazy var foo: Foo = { ... }()
  1. remove colon in after URLSession.