Print

Swift Tutorial: Introduction 05 - Variables:Integers

Written by Paul Napier on . Posted in Swift Tutorials: Introduction

Hello everyone and welcome to the fifth lesson in Swift programming, brought to you by MadApper. My name is Paul Napier, and in this lesson we are going to begin our journey into variables starting with basic types. First and foremost we are going to talk Integers.

Integers are whats commonly referred to as whole numbers. I.e. They are numbers with no fractional component, such as 34, 56, -24 or -100. In Swift you can declare integers either implicitly by declaring the variable and assigning the value as a whole number. However, if you want to declare this explicitly, you can do this by calling either:

let integer8:Int8 = 8 // 8 bits of information
let integer16:Int16 = 16 // 16 bits of information
let integer32:Int32 = 32 // 32 bits of information
let integer64:Int64 = 64 // 64 bits of information

You can access each of the minimum and maximum values in these by calling min or max i.e.

let minimum = Int16.min
let maximum = Int16.max

However, in most instances you will not even need to decide this. The simplest and most preferred way is to declare an integer with the Int type.

let integer:Int = 100
let min = Int.min
let max = Int.max

When doing this, the compiler will automatically determine the correct bit size for your system. One point to notice is the capital letter, which if you come from another language such as Objective C or Java, may throw you. All types In Swift begin with a capital letter.

Ok so we have learned about integers, but I'm sure you know that not all numbers are whole. So in the next lesson we are going to talk about floating point numbers.

Add comment


Security code
Refresh