Day 2 of HackingWithSwift's 100 days of SwiftUI. Today I learnt about the different complex types in Swift. Complex types in swift are used to store multiple variables at a time whereas simple types in swift are used to store only one variable at a time.
The complex types in Swift include:
- Array
- Set
- Tuple
- Dictionary
- Enumerations(Enums)
1.Array: Arrays in Swift allow you to store values of the same types. You can retrieve those values by using the position of that value in the array. Swift like most programming languages is zero-based programming. That means the position of values in complex types starts from 0.
To create an Array, you do this
let firstArray = ["Hello","Hello", "Holla","How far", "Wetin dey sup"]
To retrieve a value from the array, you do thisfirstArray[0]
As I said, Swift is a zero-based programming language. So the code above would retrieve the first "Hello".
You should use an array whenever you have a list of data that can contain duplicated and must be in the correct order.
2.Set: Sets in swift allow you to store unique values of the same types. Whenever you try to insert a value that already exists inside a Set, Swift won't allow you to do so. Sets can not contain duplicate values.
To create a Set, you do this
let firstSet = Set(["Hello","Holla","How far", "Wetin dey sup"])
Retrieving values from a Set works the same way as an Array.
You should use a Set whenever you have a list of data that should not contain duplicates and the order does not matter.
3.Tuple: Tuples in swift allow you to store a fixed amount of values of different types. You can not add or remove a value from a Tuple after creating it.
To create a Tuple, you do this
let firstTuple = (name: "Sanmi", level: 9000, Nationality: "Nigerian")
Retrieving values from a tuple work the same way as Sets and Arrays but with Tuples, you can also use the name of the value to retrieve it.
For example firstTuple.name
would retrieve "Sanmi"
You should use Tuples when you want to store a fixed amount of data.
4.Dictionary: Dictionary in Swift allows you to store values of the same type using a key instead of the value's position in the Dictionary to identify those values.
To create a Dictionary, you do this
let firstDictionary = ["Gintama" : "Sakata Gintoki", "Haikyuu" : "Shoyo Hinata"]
To retrieve a value from a dictionary, you use the key instead of its position like in Arrays and Sets
firstDictionary["Gintama"]
would retrieve "Sakata Gintoki"
You should use a dictionary whenever you want to use your key to retrieve a value.
5.Enumeration: Enumeration(Enum) is a bit different from the other complex data types I have mentioned so far. Enumeration group related values in a way that makes it easier to use.
For example, if you want to store a user gender in your app, you could use strings:
let userGender = "female"
This can be very error-prone most of the time. Instead of representing the user gender as String, you could use an Enum instead:
enum Gender {
case male,
case female
}
let userGender = Gender.female
Enums in Swift also allows you to store additional information that describes enum cases in detail. These are called Associated values.
If you are interested in taking the challenge, you can find it at https://www.hackingwithswift.com/100/swiftui
See you tomorrow ;)
Top comments (0)