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 🤷‍♀️

Eeb061c8b2816b771920da1b3e7904a3?s=128

Swift India

October 19, 2019
Tweet

Transcript

  1. Data Types and Collection Data Structures in Swift Spandana Batchu

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

    Big O Notation • Collection Data Structures

  3. None
  4. None
  5. None
  6. None
  7. None
  8. None
  9. None
  10. None
  11. None
  12. None
  13. None
  14. None
  15. None
  16. None
  17. None
  18. None
  19. None
  20. None
  21. None
  22. None
  23. None
  24. None
  25. None
  26. None
  27. None
  28. None
  29. None
  30. None
  31. None
  32. None
  33. None
  34. None
  35. None
  36. None
  37. None
  38. None
  39. None
  40. None
  41. None
  42. None
  43. None
  44. None
  45. None
  46. None
  47. None
  48. None
  49. None
  50. 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
  51. None
  52. None
  53. DEMO Download Demo Project at : https://github.com/spandanabatchu/BigONotation


  54. Collection Data Structures Arrays Dictionary Set

  55. 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
  56. Dictionary When there isn’t a particular order to what you

    need to store, but the data has meaningful association.
  57. Set When uniqueness matters, but order does not.

  58. 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"]
  59. Fundamental set operations

  60. 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]
  61. POP QUIZ

  62. What would you use to create a list of every

    author in the library?
  63. 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.
  64. How would you store the most popular book by each

    author?
  65. 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"
  66. How would you store the alphabetically sorted titles of a

    prolific author's entire body of work?
  67. 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.
  68. THANK YOU! References:
 Understanding Swift Performance - WWDC 2016
 Collection

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