DEV Community

Naveen Ragul B
Naveen Ragul B

Posted on • Updated on

Swift - Collection Types

Three primary collection types

  1. Array - ordered collections of values
  2. Set - unordered collections of unique values
  3. Dictionary - unordered collections of key-value associations.

Collection assigned to a
Variable - Mutable Collection
Constant - Immutable collection


Array

use Array<Element> or [Element] for Type Annotation, where Element is the type of values the array is allowed to store.

  • Array(repeating:, count:) - To create an Array with a Default Value
  • append(_:) - to append an element at the end
  • += - use this as an alternate to append(_:)
  • insert(_:at:) - To insert an item into the array at a specified index
  • remove(at:) - removes the item at the specified index and returns the removed item
  • removeLast() - remove the final item from an array
  • enumerated() - returns a tuple composed of an integer index and the item

example :

var shoppingList = ["Eggs", "Milk","Flour"]
for (index, value) in shoppingList.enumerated() {
    print("Item \(index + 1): \(value)")
}
// Item 1: Six eggs
// Item 2: Milk
// Item 3: Flour
Enter fullscreen mode Exit fullscreen mode

Sets

use a set instead of an array when the order of items isn’t important, or when an item needs to only appears once.

A type must be hashable in order to be stored in a set
A hash value is an Int value that’s the same for all objects that compare equally.

You can use your own custom types as set value types or dictionary key types by making them conform to the Hashable protocol.

Set<Element> - Type of set, where Element is the type of values the array is allowed to store.

use insert(_) method to add elements in set
example :

var letters = Set<Character>() 
letters.insert("a")
Enter fullscreen mode Exit fullscreen mode

empty set can be created with an empty array literal if the type information is already provided.
example :

letters = ["a","b","c"]
Enter fullscreen mode Exit fullscreen mode

Creating a Set with an Array Literal

example :

var genres: Set<String> = ["Rock", "Classical", "Hip hop"]
Enter fullscreen mode Exit fullscreen mode

or

var genres: Set = ["Rock", "Classical", "Hip hop"] //type inference
Enter fullscreen mode Exit fullscreen mode

Properties and Method

  • count - property contains size of set
  • isEmpty - property returns true if count is 0
  • insert(_:) - method to add elements in set
  • remove(_:) - removes the item if it’s a member of the set, and returns the removed value
  • removeAll() - removes all item in a Set.
  • contains(_:) - to check whether a set contains a particular item
  • sorted() - returns the set’s elements as an array sorted using the < operator.

Set Operation

  • intersection(_:) - method to create a new set with only the values common to both sets.
  • symmetricDifference(_:) - method to create a new set with values in either set, but not both.
  • union(_:) - method to create a new set with all of the values in both sets.
  • subtracting(_:) - method to create a new set with values not in the specified set.

  • == - to determine whether two sets contain all of the same values.

  • isSubset(of:) - method to determine whether all of the values of a set are contained in the specified set.

  • isSuperset(of:) - method to determine whether a set contains all of the values in a specified set.

  • isStrictSubset(of:) - or isStrictSuperset(of:) methods to determine whether a set is a subset or superset, but not equal to, a specified set.

  • isDisjoint(with:) - method to determine whether two sets have no values in common.


Dictionaries

use a dictionary when you need to look up values based on their identifier

Dictionary<Key, Value> or [Key: Value], where Key is the type of value that can be used as a dictionary key, and Value is the type of value that the dictionary stores for those keys.

A dictionary Key type must conform to the Hashable protocol, like a set’s value type.

example :

var namesOfIntegers: [Int: String] = [:]
Enter fullscreen mode Exit fullscreen mode

Properties and Method

  • count - number of items in a Dictionary
  • isEmpty - check whether count property is 0
  • updateValue(_:forKey:) - method sets a value for a key if none exists, or updates the value if that key already exists. and returns old value (optional)
  • removeValue(forKey:) - method to remove a key-value pair from a dictionary and and returns the removed value(optional)

Top comments (0)