Difference between array, set and dictionary in Swift

I am new to Swift Lang, have seen a lot of tutorials, but this is not clear - my question is, what is the main difference between the types of the Array , Set and Dictionary collection?

+5
source share
3 answers

Here are the practical differences between the different types:

Arrays are ordered lists and are used to store lists of information in cases where order is important.

For example, messages in a social network application displayed in a table can be stored in an array.

Sets differ in the sense that order does not matter , and they will be used in cases where order does not matter.

Sets are especially useful when you need to make sure that an item only appears once in a set.

Dictionaries are used to store pairs , sign and are used when you want to easily find the value using the key, as in the dictionary.

For example, you can save a list of elements and links for more information about these elements in the dictionary.

Hope this helps :)

(For more information and finding your own Apple definitions, check out the Apple manuals at https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/CollectionTypes.html )

+12
source

Detailed documentation can be found here Apple Guide . The following are some quick definitions extracted from there:

Array

The array stores values โ€‹โ€‹of the same type in an ordered list. The same value can appear in the array several times in different positions.

Set

The set stores individual values โ€‹โ€‹of the same type in the collection without a specific order. You can use a set instead of an array when the order of the elements is not important, or when you need to make sure that the element appears only once.

Dictionary

The dictionary stores associations between keys of the same type and values โ€‹โ€‹of the same type in a collection without any particular order. Each value is associated with a unique key, which acts as an identifier for this value in the dictionary. Unlike elements in an array, elements in a dictionary do not have the specified order. You use a dictionary when you need to look up values โ€‹โ€‹based on their identifier, in much the same way that a real-world dictionary is used to search for a definition for a specific word.

+4
source

The old thread is still worth talking about performance.

Given an N element inside an array or dictionary, you should consider performance when trying to access elements or to add or remove objects.

Arrays

In access, a random element will cost you the same as access to the first or last, since the elements follow one after another, therefore they are accessed directly. They will cost you 1 cycle.

Inserting an item is expensive. If you add to the beginning, it will cost you 1 cycle. Inserting in the middle, the remainder needs to be shifted. It may cost you as much as the worst-case N-cycle (average N / 2 cycle). If you join the end and you have enough space in the array, it will cost you 1 cycle. Otherwise, the entire array will be copied, which will cost you N cycles. This is why it is important to assign enough space to the array at the start of the operation.

Removing from the very beginning or the end will cost you 1. An average shift operation is required. On average, this is N / 2.

Finding an item with a given property will cost you N / 2 cycles.

Therefore, be very careful with huge arrays.

Dictionaries

While the dictionaries are disordered, they can bring you some advantages here. Since keys are hashed and stored in a hash table, any operation will cost you 1 cycle. Only an exception can find an element with a given property. This may cost you N / 2 cycles in the worst case. With smart design, however, you can assign property values โ€‹โ€‹as dictionary keys, so a search will cost you 1 cycle only no matter how many elements are inside.

+3
source

Source: https://habr.com/ru/post/1260005/


All Articles