Chapter 02: Variables & Constants in Swift

Data Types in Swift Language

Integers i.e. Int:
1, 2, 3, 199, 1000
Float & Doubles:
3.14, 1.168, M_PI
Boolean Values like Bool:
true or false
Collection of Characters like Strings:
“iOS Development”, “Apple”, “1.2”

We can create our data type using the keywords Struct & Class.

All About Variables

Here is how we declare Variables in Swift:
First, we start with the ‘var’ keyword, then ‘Identifier Name’ & ‘Initial Value.’

Here is how to Declare Integer values: Int

var thisYear = 2017
var ourAge = 20

Here is how to Declare Empty Integer

var emptyInteger = Int()

Here is how to Declare a String

var courseName = "iOS Development"
var language = "Swift"
var software = "Xcode"

Here is how to Declare an Empty String

var emptyString = String()

Here is how to Declare Double or Float

var currentSwiftVersion = 3.0

Here is how to Declare an Empty Double or Floating point variable

var emptyDouble = Double()

Here is how to Declare Boolean Value: Bool

var programmingIsFun = true

Here is how to Declare Empty Boolean value

var emptyBoolean = Bool()

The critical thing to remember:
Swift compiler knows the type of variable when you declare them, and the compiler infers types of variables.

All About Constants:
The value of constant never changes, and it will help you protect that keyword from anyone changing that value.

Here is how to declare Constants in Swift:
You will start with the ‘Let’ Keyword, then the ‘Identifier name’ equal sign & Constant Value

Here is how to Declare an Integer constant: Int

let thisYear = 2017
let ourAge = 20

Here is how to Declare a String constant

let courseName = "iOS Development"
let language = "Swift"
let software = "Xcode"

Here is how to Declare Double or Float constant

let currentSwiftVersion = 3.0

All about Operators available in Swift Language that you can use.

The assignment Operator is ‘=’, which will help you to assign value to ‘identifier name’ for your keyword

Arithmetic Operators available in Swift
Addition Operator:
Subtraction Operator:

Multiplication Operator:
Division Operator:
Modulus Operator:

Using Arithmetic Operators

var addition = 3 + 8 // Result is 11
var subtraction = addition - 10 // Result is 1, As addition value declared in above line is 11 then it subtract value 10 from addition value & assign it to subtraction keyword
subtraction = subtraction + 10 // Result is 11
subtraction += 5 // Result is 16
var division = 10 / 2 // Result is 5
var remainder = division % 5 // Result is 1

I hope this information helps you start with the Swift Programming Language and clears all your doubts about data types, variables, and constants.

Thanks & Regards
Mandar Apte

Published by Mandar Apte

Mandar is a Mumbai-based multi-disciplinary designer with UX/UI, Logo, Symbol, and Brand Identity design expertise. He currently runs his Mudrkashar Linguistic Apple iPhone, iPad, and Mac app business in the heart of Mumbai city.

Leave a comment

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.