Swift: Variables

This entry is part 1 of 41 in the series Swift

Apple introduced a new and aptly named Editor called Playground and this editor allows us to look at the output almost immediately on the right-hand side of the editor.

We will use the Playground for this series of tutorial.  This is a quick run through of Swift code and syntax.

Defining variables in Swift:

//: Playground - noun: a place where people can play

import UIKit

var str = "Hello, playground"
print(str)

var int = 10
print(int)

var dec = 10.511
print(dec)

swift1

Swift: If else statement

This entry is part 3 of 41 in the series Swift

The simple if else  statement in Swift.

//: Playground - noun: a place where people can play

import UIKit


var number = 8

if number > 10
{
    print("More than 10")
    
} else {
    print("Less than 10")
    
}

swift1

Swift: While statement

This entry is part 4 of 41 in the series Swift

If you are new to Apple’s swift like me, you might not know how to turn on the console to view the output.  For XCode, you can go to View -> Debug Area -> Activate Area.

Here we show how to use the while statement.

import UIKit


var number = 2

while (number <= 10)
{
    print(number)
    number = number + 1
    
}

swift

Swift: Escaping a variable

This entry is part 5 of 41 in the series Swift

Here number is a variable, to put the variable number inside a quoted string, we need to escape it using \.

import UIKit


var number = 2

print("The number now is \(number). ")

swift