Swift Language

var myFirstProgram: String
myFirstProgram = “Hello World

  1. create variable:
    var age

  2. declare variable:
    var age: Int

  3. Initialize (give it a value):
    age = 26

Example Code:

var age: Int
age = 26
print(“You are \(age) years old.”)

To skip specifying the type:

var name = “Ana”
print(“Great work\(name)!”)

**When you initialize a variable with a value, Swift will infer the type.

Assign a new value to the variable:

var feeling = “awesome”
// How are you feeling?
feeling = “tired”
print(“Your mood is \(feeling)!”)

Once a constant is set, its value cannot be changed.

// Contants start with let instead of var
let secondsPerMinute = 60

** Since 1 min = 60 seconds, using let (a constant) is safer because you will see and error message if you accidentally try to change its value.

Combining strings:

let appName = “Code Swift”
var review = “ is a fantastic app!”
var message = appName + review

The process of creating longer strings by including variables or constants within a string:

var language = “Swift”
print(“I am learning \(language)”)

var company  = “Apple”
let yearFounded = 1976
var str = “ was founded in “
print(company + str + String(yearFounded))

To create a constant, declare and set it’s value:

let fahrenheit: Float = 50

**Since an integer was set as the value, it is necessary to specify the type as float

let celsius: Float
let Fahranheit: Float = 50
celsius = (Fahrenheit - 32) / 1.8