Type Annotation in Swift
Written by Team Kodeco
In Swift, you can explicitly specify the data type of a variable or constant using type annotations. A type annotation is a way to specify the type of a variable or constant by adding a colon followed by the type name after the variable or constant name.
For example, to declare a variable as a string, you can use the following syntax:
var message: String = "Hello, World!"
In this example, message
is a variable of type String
that has been set to the value “Hello, World!”.
Similarly, you can use type annotations to declare a constant as an integer:
let numberOfApples: Int = 5
In this example, numberOfApples
is a constant of type Int
that has been set to the value 5.
You can think of type annotation like putting a label on a box: The label tells you what the box contains.