Slide 1

Slide 1 text

Collections in Swift Michał Waśniewski Schibsted Tech Polska

Slide 2

Slide 2 text

Collections in Swift New collection types in Swift Defining collections Using Swift with collections Value and Reference types Performance of new collection types

Slide 3

Slide 3 text

Swift implementation Swift-only: Before Swift: Array, replacing NSArray Dictionary, replacing NSDictionary Set, replacing NSSet (introduced in Swift 1.2) Foundation data structures e.g. NSArray,
 NSDictionary, NSSet Core Foundation data structures e.g. CFArray,
 CFDictionary, CFSet

Slide 4

Slide 4 text

Text Defining collections Image courtesy of adamr at FreeDigitalPhotos.net

Slide 5

Slide 5 text

Defining a collection Modern Objective-C Objective-C

Slide 6

Slide 6 text

Defining a collection in Swift Strictly with type Type by inference

Slide 7

Slide 7 text

Mutability In Foundation classes mutability is defined by object type, e.g. NSArray and NSMutableArray Swift introduced let and var keywords for defining constants and variables

Slide 8

Slide 8 text

Mutability Use let to define immutable collections and var for mutable

Slide 9

Slide 9 text

Modifying a Dictionary In NSMutableDictionary function for modifying a dictionary doesn’t return any value In Swift’s Dictionary function for updating dictionary returns previous value if it changed

Slide 10

Slide 10 text

Modifying a Dictionary Capture replaced value Check if dictionary was modified

Slide 11

Slide 11 text

Text Using Swift with collections Image courtesy of adamr at FreeDigitalPhotos.net

Slide 12

Slide 12 text

Adding arrays In Swift we can add arrays with adding operator (+) Obj-C

Slide 13

Slide 13 text

Iterating In Swift it’s easy to iterate over a collection without need to cast In Obj-C we need to cast value every time we want to iterate over an array

Slide 14

Slide 14 text

Tuples Tuples group multiple values into a single compound value.

Slide 15

Slide 15 text

Iterating using Tuples Using enumerate function we can iterate over an array and retrieve index and value in a single operation We can use tuples to iterate over a dictionary

Slide 16

Slide 16 text

Functional programming Filtering the old way Functional filtering More functions like sort, map, join, etc.

Slide 17

Slide 17 text

Text Value and Reference types Image courtesy of adamr at FreeDigitalPhotos.net

Slide 18

Slide 18 text

Value vs Reference Type Swift introduced two types - Value and Reference A Value type is a type which value is copied when it is assigned to a variable or constant, or when it is passed to a function. Reference types are not copied when they are assigned to a variable or constant, or when they are passed to a function. Rather than a copy, a reference to the same existing instance is used instead.

Slide 19

Slide 19 text

Swift colletions are Values “Swift’s String, Array, and Dictionary types are implemented as structures. This means that strings, arrays, and dictionaries are copied when they are assigned to a new constant or variable, or when they are passed to a function or method.”* This behaviour is different than Foundation classes which are implemented as classes passed by reference *Apple Inc. “The Swift Programming Language”

Slide 20

Slide 20 text

Value vs Reference Type Arrays are value types that only copy when necessary, which is only when the array itself changes (not the contents.)

Slide 21

Slide 21 text

Text Performance Image courtesy of Teerapun at FreeDigitalPhotos.net

Slide 22

Slide 22 text

Performance in arrays Creating NSArray objects grows at about an O(n) Creating Swift Array grow between O(n) and O(n²) Creating arrays Looking up items in array Looking up by index is almost the same, but looking by object is much faster in Swift’s Arrays http://www.raywenderlich.com/79850/collection-data-structures-swift

Slide 23

Slide 23 text

Performance in dictionaries Creating NSDictionary is also faster than creating Swift Dictionary, it’s performance degrades between O(n) and O(n²) Creating dictionaries Looking up items It’s twice faster than NSDictionary http://www.raywenderlich.com/79850/collection-data-structures-swift

Slide 24

Slide 24 text

Should I use new collection types?

Slide 25

Slide 25 text

Thanks! Questions?