When declaring a variable, we can include a type annotation in Swift to specify what kind of value the variable can hold. So, type annotation in Swift programming language is very useful for this purpose.
The following are some examples of basic data types:
Int: integer numbers
Bool: true/false values
Double: floating-point numbers
String: sequence of characters
Type annotations begin with a colon
: and end with a type in Swift.
To declare a variable named
writer and assign it the
var writer: String
Because it identifies the type of a variable as
String, we can now only assign string values to
writer = "John Hardy";
The following code can be written in a single line as well:
var writer: String = "John Hardy";
Note: If you try to assign a decimal value to an integer variable, you’ll get unexpected results, warnings, or errors.
Here are some useful tools to help you along your journey!
Setting up an IDE (Integrated Development Environment) can be difficult for beginners. The Online Compiler will enable you to run your code inside your browser without the need to install an IDE. If you need a more detailed explanation of a specific topic, the best place to find answers is in the Official Documentation.