We can provide a type annotation when we declare a variable to be clear about the kind of value that the variable can store.
Some of the basic data types:
Int: integer numbers
Double: floating-point numbers
String: a sequence of characters
Bool: true/false values
Type annotations begin with a colon
: and end with a type.
To declare a variable named
artist and give it the type
var artist: String
It explicitly specifies the type of a variable to be
String, so now we can only give
artist text string values:
artist = "Daniel Johnston"
We can also write the above code in one line:
var artist: String = "Daniel Johnston"
Note: Swift is known as a strongly typed language. If you try to give an integer variable a decimal value, you are going to get unexpected results, warnings, or errors.
String variable named
book with a value of
Int variable named
pageCount with a value of