Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Array, Set and Dictionary in Swift

Tags:

I am new to Swift Lang, have seen lots of tutorials, but it's not clear – my question is what's the main difference between the Array, Set and Dictionary collection type?

like image 341
Magesh Pandian Avatar asked Nov 19 '16 10:11

Magesh Pandian


People also ask

What is the difference between array and set in Swift?

Arrays are ordered collections of values. Sets are unordered collections of unique values. Dictionaries are unordered collections of key-value associations. Arrays, sets, and dictionaries in Swift are always clear about the types of values and keys that they can store.

What is a dictionary in Swift?

Dictionaries are unordered collections of key-value associations. Arrays, sets, and dictionaries in Swift are always clear about the types of values and keys that they can store. This means that you can’t insert a value of the wrong type into a collection by mistake.

What is the difference between a dictionary and an array?

Sets are unordered collections of unique values. Dictionaries are unordered collections of key-value associations. Arrays, sets, and dictionaries in Swift are always clear about the types of values and keys that they can store. This means that you can’t insert a value of the wrong type into a collection by mistake.

What is the full form of array in Swift?

The type of a Swift array is written in full as Array<Element>, where Elementis the type of values the array is allowed to store. You can also write the type of an array in shorthand form as [Element].


2 Answers

Here are the practical differences between the different types:

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

For example, posts in a social network app being displayed in a tableView may be stored in an array.

Sets are different in the sense that order does not matter and these will be used in cases where order does not matter.

Sets are especially useful when you need to ensure that an item only appears once in the set.

Dictionaries are used to store key, value pairs and are used when you want to easily find a value using a key, just like in a dictionary.

For example, you could store a list of items and links to more information about these items in a dictionary.

Hope this helps :)

(For more information and to find Apple's own definitions, check out Apple's guides at https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/CollectionTypes.html)

like image 163
GJZ Avatar answered Sep 20 '22 18:09

GJZ


Detailed documentation can be found here on Apple's guide. Below are some quick definations extracted from there:

Array

An array stores values of the same type in an ordered list. The same value can appear in an array multiple times at different positions.

Set

A set stores distinct values of the same type in a collection with no defined ordering. You can use a set instead of an array when the order of items is not important, or when you need to ensure that an item only appears once.

Dictionary

A dictionary stores associations between keys of the same type and values of the same type in a collection with no defined ordering. Each value is associated with a unique key, which acts as an identifier for that value within the dictionary. Unlike items in an array, items in a dictionary do not have a 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 look up the definition for a particular word.

like image 20
Munahil Avatar answered Sep 21 '22 18:09

Munahil