Learn

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 String:

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.

Instructions

1.

Declare a String variable named book with a value of "Just Kids".

2.

Declare an Int variable named pageCount with a value of 278.

Take this course for free

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?