Chapter 02: Let’s Learn Swift Programming Language – 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 own data type using keywords Struct & Class.

All About Variables

Here is how we declare Variables in Swift: First we start with ‘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 String

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

Here is how to Declare Empty String

var emptyString = String()

Here is how to Declare Double or Float

var currentSwiftVersion = 3.0

Here is how to Declare 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()

Important thing to remember: Swift compiler knows type of variable when you declare them as types of variables are inferred by compiler.

All About Constants: Value of constant never change & it will help you to protect that keyword from anyone changing that value.

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

Here is how to Declare Integer constant: Int

let thisYear = 2017
let ourAge = 20

Here is how to Declare 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.

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 to get started with Swift Programming Language & clear all your doubts about data types, variable & constants.

Thanks & Regards
Mandar Apte

Mandar Apte

This website contains a design articles blog by Mandar Apte. He writes and shares his iOS development, graphic, web, & animation film design experience through articles. Mandar is Mumbai based multi-disciplinary designer with expertise in UI, UX, Logo, Symbol, and Brand Identity design. He is an alumnus of Sir J. J. Institute of Applied Art, Mumbai. He currently runs his studio in the heart of the city of Mumbai.

Leave a Reply

Your email address will not be published. Required fields are marked *