Slide 1

Slide 1 text

Data Types and Collection Data Structures in Swift Spandana Batchu

Slide 2

Slide 2 text

Agenda • Class Vs Struct - Dimensions of Performance
 • Big O Notation • Collection Data Structures


Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

No content

Slide 23

Slide 23 text

No content

Slide 24

Slide 24 text

No content

Slide 25

Slide 25 text

No content

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

No content

Slide 28

Slide 28 text

No content

Slide 29

Slide 29 text

No content

Slide 30

Slide 30 text

No content

Slide 31

Slide 31 text

No content

Slide 32

Slide 32 text

No content

Slide 33

Slide 33 text

No content

Slide 34

Slide 34 text

No content

Slide 35

Slide 35 text

No content

Slide 36

Slide 36 text

No content

Slide 37

Slide 37 text

No content

Slide 38

Slide 38 text

No content

Slide 39

Slide 39 text

No content

Slide 40

Slide 40 text

No content

Slide 41

Slide 41 text

No content

Slide 42

Slide 42 text

No content

Slide 43

Slide 43 text

No content

Slide 44

Slide 44 text

No content

Slide 45

Slide 45 text

No content

Slide 46

Slide 46 text

No content

Slide 47

Slide 47 text

No content

Slide 48

Slide 48 text

No content

Slide 49

Slide 49 text

No content

Slide 50

Slide 50 text

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

Slide 51

Slide 51 text

No content

Slide 52

Slide 52 text

No content

Slide 53

Slide 53 text

DEMO Download Demo Project at : https://github.com/spandanabatchu/BigONotation


Slide 54

Slide 54 text

Collection Data Structures Arrays Dictionary Set

Slide 55

Slide 55 text

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

Slide 56

Slide 56 text

Dictionary When there isn’t a particular order to what you need to store, but the data has meaningful association.

Slide 57

Slide 57 text

Set When uniqueness matters, but order does not.

Slide 58

Slide 58 text

var favoriteGenres: Set = ["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"]

Slide 59

Slide 59 text

Fundamental set operations

Slide 60

Slide 60 text

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]

Slide 61

Slide 61 text

POP QUIZ

Slide 62

Slide 62 text

What would you use to create a list of every author in the library?

Slide 63

Slide 63 text

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.

Slide 64

Slide 64 text

How would you store the most popular book by each author?

Slide 65

Slide 65 text

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"

Slide 66

Slide 66 text

How would you store the alphabetically sorted titles of a prolific author's entire body of work?

Slide 67

Slide 67 text

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.

Slide 68

Slide 68 text

THANK YOU! References:
 Understanding Swift Performance - WWDC 2016
 Collection Data Structures in Swift - Raywenderlich Download Demo Project at : https://github.com/spandanabatchu/BigONotation