Collection of Data in Swift
Arrays, sets, tuples, dictionaries, and enums
Do you have any interest in iOS development although currently as a Java developer like me? :) This article will contain some notes about the collection of data in Swift and some differences & similarities to Java.
Arrays
Of course, it’s not possible to say that arrays in Swift are exactly the same as in Java, but it’s pretty close. For example, reading an item is the same as Java, you should just write the array’s name and numerical position of the item inside square brackets. In the same way, arrays can contain duplicate values just like in Java, and the order of items matters. And also, numerical positions start to count from “0” either.
As for differences, we can say that creating an array, adding/removing an item to/from an array, and syntax differences. (e.g. brackets, semicolon)
Sets
Sets are just like arrays with a few differences.
- All values must be unique. Duplicate values get ignored.
- Sets are unordered. It is not able to read a value using numerical positions.
- Sets can be very useful when you need to find quickly if a value is in a large dataset.
In the below code block, both of the sets include the same values. It does not matter to have two “cat” and “ dog” items in the animalsDuplicate.
Sets are very similar to HashSet in Java. However, there is no chance to access the first and last items in Swift like Java HashSet.
Tuples
Basically, Tuples store different values as a single value like the others as well. However, there are some obvious differences.
- Numerical positions can be used to access an item, unlike sets.
- Items have their unique names. Swift allows reading items with also using their names.
- The types of values are always the same as they were created with. Can not be changed later.
- Tuples’ sizes are fixed. It is not possible to add or remove any item.
- Tuples can contain different types of value.
Dictionaries
Dictionaries are another way to collection of data using data pairs called keys and values. The type of items must be the same for both keys and values. Also, they are unordered such as sets.
In the example below, cities are called keys and they are used to read values in the dictionary.
Trying to read a non-exist value from a dictionary can cause issues in the code. To avoid that, default
value is used while reading values.
Enums
Enums (Enumerations) is the way of storing related values together. This is one of the important features of Swift.
- In general, they are used to mean something specific and secure.
- They provide type-safe.
- They are much faster than arrays.
- When you use enums, you must choose one of the values.
Enums are usually used with associated values to provide more detailed information. Associated values can be attached to every case or only some cases. Also, each case can have more than one associated value as needed. In the below example; depth
,position
and book
are the associated values.
If you need to be able to assign values to enums so they have meaning, rawValue
can be used.
If I didn’t give to case monday
a value of 1
, swift would automatically start at 0 and I would have to call rawValue: 1
for tuesday
. However, with the specific value that I gave to monday
, now it’s much more sense. Monday is the first day, Tuesday is the second…
In summary, structures of collection data, which actually serve to keep multiple values as a single value, are not completely independent and different from each other in Swift and Java programming languages. Even though they have strengths or weaknesses against each other, we can say they have similar features in the first place. Of course, It should also be noted that the differences in usage increase as you go deeper.
Thank you for reading. I hope this article can help you on your own journey. :)
Thanks to the ZeroToHero_iOS team! :)