Variables

Dictionaries in Swift

Used to store key-value pairs.


Syntax
//declaration
var dictionaryName = [keyDataType: valueDataType]()
//inserting values
dictionaryName[key] = value

//predefined
var dictionaryName: [keyDataType: valueDataType] = [key1: value1, key2: value2, ..]

//update key value

Notes

Similar to a hash map in Java, or an object key-value store in Javascript.

A key value pair can be updated using the updateValue function. This function returns an optional of the old value's type.


Example
days_of_week: [String: String] = {'Monday': 'mon', 'Tuesday': 'tues'}

days_of_week['Wednesday'] = 'wed' #add a key
print ("\(days_of_week['Monday'])") #will output 'mon'

< Arrays   |   Sets >

© 2019 SyntaxDB. All Rights Reserved.