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

Data Types and Collection Data Structures in Swift

Data Types and Collection Data Structures in Swift

Swift Hyderabad Chapter 4
Speaker: Spandana Batchu

Abstract: Swift has brought many performance improvements to Swift data structures and outperforms Foundation data structures in the majority of cases. Let's learn about what are the “best” ones to use, based on the operation you want to perform and what big-o-notation is all about.

Bio: Swift Developer, Senior iOS Engineer 👩‍💻, Yoga Trainer 🤸‍♀️, Loves Dogs 🐶 & clearly an emoji Lover 🤷‍♀️

Swift India

October 19, 2019
Tweet

More Decks by Swift India

Other Decks in Technology

Transcript

  1. Agenda • Class Vs Struct - Dimensions of Performance
 •

    Big O Notation • Collection Data Structures

  2. Big O Notation O(1) Constant time O(log n) Logarithmic O(n)

    Linear O(n (log n)) Linearithmic O( n ) Constant time 2 O(2^n) Exponential O(n!) Factorial
  3. Array Use Array when the order of variables matters Creating

    an Array with a default value: var threeDoubles = Array(repeating: 0.0, count: 3) // threeDoubles is of type [Double], and equals [0.0, 0.0, 0.0] It is good practice to create immutable collections in all cases Where the collection does not need to change
  4. Dictionary When there isn’t a particular order to what you

    need to store, but the data has meaningful association.
  5. var favoriteGenres: Set<String> = ["Rock", "Classical", "Hip hop”] // favoriteGenres

    has been initialised with three initial values Set A set type cannot be inferred from an array literal alone, so the type Set must be explicitly declared. var favoriteGenres: Set = ["Rock", "Classical", "Hip hop"]
  6. let oddDigits: Set = [1, 3, 5, 7, 9] let

    evenDigits: Set = [0, 2, 4, 6, 8] let singleDigitPrimeNumbers: Set = [2, 3, 5, 7] oddDigits.union(evenDigits).sorted() // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] oddDigits.intersection(evenDigits).sorted() // [] oddDigits.subtracting(singleDigitPrimeNumbers).sorted() // [1, 9] oddDigits.symmetricDifference(singleDigitPrimeNumbers).sorted() // [1, 2, 9]
  7. A Set! It automatically removes duplicate names, which means you

    can enter every single author's name as many times as you want, but you'll still have only a single entry. Once you create the Set, you can use set.allObjects to access the Array of unique names.
  8. A Dictionary! If you use the author's name as the

    key and the title of that author's most popular book as the value, you can access the most popular book by any author like this: mostPopularBooks["Gillian Flynn"] //Returns "Gone Girl"
  9. How would you store the alphabetically sorted titles of a

    prolific author's entire body of work?
  10. An Array! Since you're in a situation where you have

    a number of similar objects (all titles are Strings) and their order matters (titles must be sorted alphabetically) this is an ideal case for an Array.
  11. THANK YOU! References:
 Understanding Swift Performance - WWDC 2016
 Collection

    Data Structures in Swift - Raywenderlich Download Demo Project at : https://github.com/spandanabatchu/BigONotation