Swift Variables, Basic Intro

Swift Variables

Some basic information about Swift variables. Nothing to complicated if you programmed before, no matter what language you used. Variables are variables in any language. Anyway, some basic stuff about Swift variables.


Integers are whole numbers. Like 1 or 5 or even 1000. Integers can also of course be a negative number like -54. Unless you are working with unsigned integers. Those are positive integers only.

How to declare a integer in Swift?
var myLuckyNumber = 7

Or you can declare it like this:
var myOtherLuckyNumber: Int = 77

This last method is not really needed as long as you use a number. Swift will know what kind of datatype you are declaring. If however you want to declare an integer without assigning a value to it. You will need to specify the datatype, like integer. You use the second method just with out the equal sign and number. Like this:
var myNewInteger: Int

And if you want your integer to be a constant. Not being able to change its value, you replace the var keyword with let. Example:
let myIntegerConstant = 3.14


Doubles are numbers with decimals. Like pi (3.14) is a Double. And 5.64 just to give a few examples. -104.5 is also a Double.

How to declare a Double in Swift?
var myUnluckyNumber = 18.98

Or you can specify double, like this:
var myDoubleNumber:Double = 56.999

Again, it is not needed to specify double as long as you use the decimal number in the declaration.


Booleans (or bool) are either true or false. They can only hold true or false.

Example bool constant:
let iAmSoHappy = true

or like this:
var restartGame:Bool = false

They are useful for “flags” to trigger events in your programs. Like in the example above. To tell your game to restart or when it is game over, level completed and things like that.


String is another swift variables, and in any other programming language I would guess. Strings are text. Just text or characters really.

Example on how to declare a string variable in swift:
var myString = “This is a typical string”

or the second method:
var myString:String = “This is another string”

They can also be constant, like all other datatypes.

This is just a few examples on swift variables. There are more datatypes, which you can find in the Apple documentation. Just click the link there.

Thats it for now.

Happy variables.

About Author

Related Posts

C# Reference Types

Understanding C# Reference Types

One of the key features of C# is its support for C# reference types, which allow developers to create complex, object-oriented applications. In this blog post, we…

c# value types

Understanding C# Value Types

C# is a powerful programming language that is widely used for developing a wide range of applications. One of the key features of C# is its support…

C# check if server is online

C# check if server is online directly from your code. Check servers or services like web servers, database servers like MySQL and MongoDB. You can probably check…

C# Convert Int to Char

C# convert int to char google search is giving some result which I think is not directly what some people want. Most results are giving instructions on…

c# bash script

C# Bash Script Made Easy

There are many reasons why it could be handy to run a bash script from a C# application. In 2014, before I changed to a Mac as…

go hello world

Golang Hello World, Get a Easy Fantastic Start

Golang hello world example tutorial. I assume you are new to the golang language since you found this website. Go is one of the latest programming languages…

Leave a Reply