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

Collection data types in Swift

Collection data types in Swift

mwasniewski

March 31, 2015
Tweet

More Decks by mwasniewski

Other Decks in Programming

Transcript

  1. Collections in Swift New collection types in Swift Defining collections

    Using Swift with collections Value and Reference types Performance of new collection types
  2. 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
  3. 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
  4. 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
  5. 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
  6. 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
  7. 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.
  8. 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”
  9. Value vs Reference Type Arrays are value types that only

    copy when necessary, which is only when the array itself changes (not the contents.)
  10. 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
  11. 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