Using Collections Effectively

Description: Every app uses collections! Go beyond the basics with specific tips on how best to use indices, slices, bridging, laziness, and reference types. Gain better understanding of when to use each collection for best performance.

  • Swift protocols for collections represent a good starting point for thinking about collection operations in swift.
  • Numerous types conform to these protocols (IndexPaths, Data etc..)
  • Slicing and Lazy collections represent optimizations and deferred operations but understanding the underlying memory semantics is important.
  • Mutable collections should be avoided and holding onto calculated indexes inherently dangerous when dealing with mutable collections
  • Need to work with Thread Sanitizer TSAN more.
  • Collections are optimized for single thread access (but thread safe right?)
  • Bridging between objective-c and swift can affect performance. Look for bridge api calls and perf in Time Profiler.
  • NS~Collection types (ref type) vs Swift collections.
  • Use foundation collections (NS..) when you reference semantics and the elements are known to be reference types.

Missing anything? Corrections? Contributions are welcome 😃


Written by

Federico Zanetello

Federico Zanetello

iOS Engineer with strong passion for Swift, minimalism, and design. When he’s not busy automating things, he can be found writing at FIVE STARS and/or playing with the latest shiny toys.