#2-Learn Swift-Constants and Variables

Today you will learn the difference between constants and variables.

You will see the difference between constants and variables, as you look at the picture below. 

Konstanten und Variablen

Before you can use your constants and variables, you need to declare them.

To declare a variable, you use the expression var.

var ActualWeather = "Sunny"

A constant is declared through the keyword let.

let UmbrellaColor = "Red"


Other types of values

At the moment, we just stored a String in a constant and variable. In the following, you will learn which other types of values can be stored.

Boolean (Can assume the following values: true or false)

var Visible = false

let TheEarthisasphere = true

Integer (Can just assume integers)

Integers have min and max properties (The significance of min and max properties reveal itself, after you read the paragraph about type annotations):

var Currenttemperature = 25

let Lastbillingamount = 10

Double (Can assume integers and decimal numbers)

var Testvalue1 = 2.44

let Testvalue2 = 5

Float (Can assume integers and decimal numbers)

var Testvalue3 = 3

let Testvalue4 = 7.86

If you have to calculate with decimal numbers, remember that a Double is more precise than a Float!


Type annotations

If you are sure about the kind of values the constant or variable should store, you can use a type annotation:

var Visible:Bool = true

var Text:String = "This is a sample text"

var Number:Int = 8  or  var Number:Int8 = 5

var Firstdecimalnumber:Double = 2.1

var Seconddecimalnumber:Float = 2.4

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s