Difference between revisions of "Dictionaries"

From Coder Merlin
m (Chukwuemeka-tinashe moved page W2273 Dictionaries to Dictionaries without leaving a redirect: Improved navigation)
(No difference)

Revision as of 09:04, 15 October 2021

Within these castle walls be forged Mavens of Computer Science ...
— Merlin, The Coder

Dictionaries[edit]

Arrays are a collection type and enable us to quickly and easily access an element of the collection at a specific index, identified by an integer offset from the beginning of the collection. While arrays are very fast, they don’t provide us with the ability to quickly access an element given a key of arbitrary type.

Dictionaries are a type of hash table. A hash table is a data structure that implements the abstract data type associative array. The hash table uses a hash function to compute an index into an array of slots at which the element can be found. In a well-implemented algorithm, access to the element can occur in O(1).

In some cases, a hash function may not produce unique values. This is termed a collision. Collisions need to be appropriately resolved in order to access the proper element.

The structure of unordered lists in Swift is accomplished via unordered lists. Even if you make an error when entering a value into a dictionary, Swift will not allow it.

A key is a number used to identify each item in a dictionary. A value is stored in the unique key, which may be referenced and looked up using the same key later. Unlike items in an array, values in a dictionary do not have to be kept in any particular order. When you need to find values based on their identifiers, use a dictionary instead of an array.

A key in a dictionary is unrestricted, although it must be unique within the dictionary.

Because a variable receives a new dictionary as an assignment, it is always mutable, allowing you to add, remove, or modify its entries. If you assign a constant to a certain value, the dictionary becomes immutable and unchangeable in size and content.

Curriculum[edit]

ExercisesIcon.png
 Coder Merlin™  Computer Science Curriculum Data

Unit:

Experience Name: Dictionaries ()

Next Experience: ()

Knowledge and skills: noneSome use of "" in your query was not closed by a matching "".

Topic areas: none

Classroom time (average):

Study time (average):

Successful completion requires knowledge: none

Successful completion requires skills: none

Creating Dictionaries in Swift[edit]

Swift allows you to create an empty dictionary of a specific type.

If you need an Int dictionary but aren't sure what the key should be, you may use a different syntax to create one with an empty key of type Int and string values.

//Basic syntax
var myDictionary = [key: value]()

//Example of dictionary creation
var myDictionary:[Int:String] = [1:"First", 2:"Second", 3:"Third"]

Accessing Elements in Dictionaries[edit]

The subscript syntax may be used to access a value from a dictionary, as follows: -> You may access a value from a dictionary using subscript syntax by entering the key of the desired item inside square brackets right after the name of the dictionary.

CoderMerlin™ Code Explorer: W0000 (1) 🟢


Updating Elements in Dictionaries[edit]

You may use the updateValue(forKey:) method to add a current value to a given key in the dictionary. This method returns an optional element from the dictionary's value type.

CoderMerlin™ Code Explorer: W0000 (2) 🟢


Removing Key-Value Pairs from a Dictionary[edit]

To remove a key-value pair from a dictionary, you may use the removeValueForKey() function. If the pair exists, it is deleted and the value returned; otherwise, nil is returned. Here's an example that shows how to use it:

CoderMerlin™ Code Explorer: W0000 (3) 🟢


Conclusion[edit]

Dictionaries are very helpful when programming in Swift because they provide a quick way to access data. However, they also have some pitfalls that programmers should be aware of.

First, the implementation of a dictionary leaves values unguarded to some extent and programmers should take precautions against this.

Second, programmers should avoid retrieving data from an uninitialized or partially initialized dictionary as this can lead to unexpected behavior.

Third, programmers should be aware that accessing a key in a dictionary is ordered which means it will not be as fast as other collection types such as arrays.

Fourth, if dictionaries are used extensively throughout an app then those applications may suffer from slow performance due to those large amounts of allocations occurring on the heap.

Finally, one thing that I haven't mentioned much in this post but is worth mentioning is how using let generally affects performance because it prevents variables and constants from being put into the microprocessor's cache (the small amount of memory that is much faster than the main memory).