Swift Basics: Type Annotations

Swift Basics: Type Annotations

Hi All, As I told in the previous articles that I have started a #100DaysOfSwiftUI challenge this is my learning from Day 4. where I have learned about Type Annotation in Swift and its use cases. Let's go through it once:

Type Annotation

Swift figures out the type of data assigned to the variable based on the value we assigned to that variable. When we assign a String value to a variable it will be a String variable or when we assign an integer value to a variable it will become an Integer variable and so on. However sometimes we can't assign a value to a variable right away or sometimes we want to override Swift's choice of type, and that's where type annotations come in.

So far we've been making constants and variables like this:

let name = "Coding Monkey"
var age = 22

So as I explained earlier we know the type of variable will be known once the value is assigned to the variable. So let's consider a case where we don't know the value to be assigned to that variable but we know that what type of data we are expecting and we use type annotation like this:

let name: String = "Coding Monkey"
var age: Int = 22

We are explicitly telling that name must be of type string and age should be of type Int. Let's try to define all the types of data types we learned so far for basic data types using type annotations.

// For String data type which holds text
let name: String = "Coding Monkey"

//For Int which holds whole numbers:
var age: Int = 22

//For Double which holds decimal number:
let pi: Double = 3.141

//For Boolean which holds either true or false
var gameOver: Bool = true

Now let's try a negative scenario where we try to assign different data types the Xcode will throw an error message to assign proper value.

//Try assigning a different data type than defined type annotation
var age: Int = 44.5

Now let's try using the type annotation for complex data types which we have gone through on last post.

//Array which holds different value but of same data type
var animals: [String] = ["Monkey", "Cat","Dog"]

//Dictionary which holds data in key-value pairs
var user:[String: String] = ["name": "Coding Monkey"]

//Set which holds different data in no particular pattern but which is optimised for better performance
var scores: Set<Int> = Set([98, 99, 94, 95])

So in the above code snippet, I have shared all the complex data types which we learned in last post except for the type Enum which we will go through now since Enums are a little different from others since it let us create a new type of our own such as enum containing days of the week, an enum containing which UI theme the user wants, or even an enum containing which screen is currently showing in our app.

Values of an enum have the same type as the enum itself, so we could write something like this:

enum UIStyle {
          case light, dark, system
}

var style = UIStyle.light

So this allows Swift to remove the enum name for future assignments, so we can write style = .dark- it knows any new value for style must be some kind UIStyle.

Use cases of Type Annotations

So for one of these three reasons, we will be using the type annotation:

  • Swift can't figure out what type should be used.
  • You want Swift to use a different type from its default.
  • You don't want to assign a value just yet.

First type will be happening once more lines of code are added and the code gets more advanced. for example, we will be getting data from some other place like downloading from the internet or the result of some logic implemented.

Second scenario is quite common as we learn more of Swift. So for example creating a double without adding decimal.

var percentage: Double = 99

Third option is you are telling Swift that there will be a value but can only get it in the future.

var value: String

This kind of code requires a type annotation because without an initial value being assigned Swift doesn’t know what kind of data username will contain.

Since we have gone through what type annotation is, Now let's see when to use type annotations, so it might be helpful for you to know that I prefer to use type inference as much as possible, meaning that I assign a value to a constant or variable and swift chooses the correct type automatically.

the most common exception is when we don't have the value for the variable yet. You see, Swift is clever: we can create a constant or variable now for which we don't have a value yet, later on, we can provide that value and swift will ensure we don't accidentally sure if until a value is present, it will also ensure that you only ever set the value once so that it remains constant.

let username: String
//after some line of code
username = "@coding_monkey"
// after a few more lines of code
print(username)

Conclusion

So I have learned on the Day 4 of 100 Days of SwiftUI that how to use Type Annotation in Swift and use cases for type annotation.

Also, let me know if any mistakes are done in this blog/write-up I have done on Day 4 learning so I can fix my mistakes and improve my understandings.

Thank you.