Swift101: Collection Types
Hi. Today, I would like to talk about Collection Types.
The collection type is the data type for storing multiple elements in a single variable. There are several collection types in swift, but three important ones are array, dictionaries and sets.
Array
What is an array?
- It’s an ordered collection
- It’s store multiple elements of the same type
- The element in the array are zero-indexed(the index of the first element is 0)
Creating arrays
- You can create an array with
array literal.
Access elements
- You can access an element in an array with
subscript syntax
Slicing array with countable ranges
- You can use the subscript syntax with countable ranges to fetch more than a single value from an array.
Appending elements
If you want to add an element to the end of the array. Swift language provides 2 way for performing that, append(_:) and +=
operator
- append(_:)
- += operator
as the example above, if you try to append anything other than string. The compiler will show an error…
error: cannot convert value of type ‘Int’ to expected argument type ‘String’
Inserting elements
If you want to add an element into an array with a specific position, you can use the method insert(_:at:)
for performing that.
The at
argument defines where you want to add the element. Remember that the array is zero-indexed
Iterating element on array with for..in
as the example above, this is an iterate value from an array. if you need the index of each element, you can iterate over the return. value of the array’s enumerated()
method.
Dictionaries
What is dictionaries ?
- It’s an unordered collection of pairs
- Each pair comprises a key and a value
- Keys on dictionary is unique (The same key can’t appear twice in a dictionary)
Creating dictionaries
- Using a
dictionary literal
- Dictionary literal is a list of key-value pairs separated by commas and enclosed in square brackets.
When you print the dictionary, there’s no particular order to the pairs because the dictionary is unordered.
If you would like to create an empty dictionary, you can define the dictionary with [:]. Remember that, you must explicit data type, otherwise, its compiler can’t inter the type.
Accessing values
- Using
subscripting syntax
for access value from the dictionary. - If the dictionary does find the key, it will return Optional type
Optional(value)
. - If the dictionary doesn’t find the key, it will return
nil.
Modify Dictionary
- You can using
subscripting syntax
for adding a new pair, update a value, and remove a pair
Adding a value
Updating values
Subscripting syntax like a
upsert
. upsert mean’s if does find a key on dictionary, it’s update value but if does’t find a key on dictionary, it’s adding a new pair.
Remove a pair
Iterating Pairs
- You can using
for-in
for iterating over a dictionary. - The items in a dictionary are pairs, you need to use a tuple
If you would like to iterate over just the values, you can using values
property and If you would like to iterate over just the keys, you can using keys
property.
Sets
What is Sets ?
- It’s an unordered collection of unique value of the same type
Creating set
- You can declare a set explicitly by writing struct
Set
followed by the type inside angle brackets
or you can using Set([element])
without explicit type
Conclusion
Array
- Are ordered collections of values of the same type
- Use
subscripting syntax => arr[index]
to access and update element - Be wary of accessing an index that’s out of bounds
Dictionaries
- Are unordered collections of key-value pairs.
- The keys and values are all of the same type
- Use
subscripting syntax => arr[key]
to get values, added a new pair, update value by key, and remove pair. - The keys on dictionary is unique
Sets
- Are unordered collections of unique values of the same type