in , ,

Swift Variables, Constants and Literals

Swift Variables, Constants and Literals
Swift Variables, Constants and Literals

Swift variables constants literals: In this tutorial, you will learn about variables, constants, and literals in Swift.

1. Swift Variables

What is a Variable?

In programming, variables are used to store information in memory which can be used all through the program. Every variable should be given an extraordinary name called an identifier. It is useful to consider variables as containers that hold data that can be changed later.

Non actually, you can consider a variable a bag to store a few books in it and those books can be replaced with different books later.

In programming, a variable is a container (storage area) to hold information. For instance,

var num = 10 

Here, num is a variable storing the value 10.


Declare Variables in Swift

In Swift, we use the var catchphrase to declare variables. For instance,

var siteName:String
var id: Int

Here,

  • siteName is a variable of type String. This means it can just store text-based values.
  • id is a variable of Int type. This means it can just store integer values.

Note: In Swift, we can’t change the type of a variable whenever it’s declared.


Assign Values to Variables

You can assign values to variables using the = operator.

var siteName: String
siteName = "worldofitech.com"

print(siteName) 

Output

worldofitech.com

You can likewise assign a variable straightforwardly without the sort annotation as:

var siteName = "worldofitech.com"
print(siteName) // worldofitech.com

Here, the compiler automatically sorts out that siteName is a variable of the String type.


Change Value of a Variable

You can change the value of an existing variable. Thus, the name variable. For instance,

var siteName = "worldofitech.com"
 
// assigning a new value to siteName
siteName = "healthsplatform.com"
print(siteName)

Output

healthsplatform.com

Here, the value of siteName is changed from “worldofitech.com” to “healthsplatform.com”.


Rules for naming Swift Variables

The rules for naming variables are:

  1. Variables names should begin with either a letter, an underscore _, or the dollar sign $. For instance,
// valid
var a = "hello"
var _a = "hello"
var $a = "hello"

2. Variable names can’t begin with numbers. For instance,

// invalid
var 1a = "hello" // throws error

3. Swift is case-sensitive. So A and a are various variables. For instance,

var A = 5 
var a = 55
print(A) // 5
print(a) // 55

4. Abstain from using Swift watchwords like var, String, class, and so on as variables names.

Notes:

It’s a decent practice to give a descriptive variable name. For instance, numberofApples is a preferred variable name over a, apple, or n.

In Swift, variable names are for the most part written in camelCase on the off chance that they have numerous words. For instance, myVariable, addTwoNums, and so on


2. Swift Constants

What is a Constant?

A constant is a special type of variable whose value can’t be changed. It is useful to consider constants as containers that hold data that can’t be changed later.

Non technically, you can consider of constant as a bag to store some books and those books can’t be replaced once positioned inside the bag.

A constant is a special type of variable whose value can’t be changed. For instance,

let a = 5

Here, after a is initialized to 5, we can’t change its value.


Declare Constants in Swift

In Swift, we use the let catchphrase to declare constants. The value of a constant can’t be changed. For instance,

let x = 5
x = 10      // Error
print(x)

Output

main.swift:4:1: error: cannot assign to value: 'x' is a 'let' constant

Likewise, you can’t declare a consistent without introducing it. For instance,

let siteName: String
print(siteName)

Output

main.swift:4:7: error: constant 'siteName' used before being initialized

Notes:

On the off chance that you are certain that the value of a variable will not change all through the program, it’s recommended to use let.

The rules for naming variables likewise apply to constants.


3. Swift Literals

What is a Literal?

An exacting is a value that shows up straightforwardly in your source code. It can be a number, character, or a string and so forth For e.g: “Hello, World” , 12, 23.0, “C” are basic illustration of literals. Literals are often used to instate (assign values to) variables or constants.

Literals are representations of fixed values in a program. They can be numbers, characters, or strings, and so forth For instance, “Hello, World!”, 12, 23.0, “C”, and so forth

Literals are often used to assign values to variables or constants.

For instance:

let siteName = "healthsplatform.com"

In the above expression, siteName is a variable, and “healthsplatform.com” is a literal.


Integer Literals

Integer literals are those that don’t have a partial or an exponential part.

There are four sorts of integer literals in Swift:

TypeExampleRemarks
Decimal5, 10, -68Regular numbers.
Binary0b101, 0b11Start with 0b.
Octal0o13Start with 0o.
Hexadecimal0x13Start with 0x.

Floating-point Literals

Floating-point literals are numeric literals that have floating decimal focuses. For instance,

let piValue: Float = 3.14

Here, 3.14 is a floating-point literal assigned to the piValue constant.


Boolean Literals

There are two boolean literals: true and false.

For instance,

let pass: Bool = true 

Here, true is a boolean literal assigned to pass.


String and Character Literals

Character literals are Unicode characters enclosed in double quotes. For instance,

let someCharacter: Character = "S"

Here, S is a character literal assigned to someCharacter.

Also, String literals are arrangements of characters enclosed in double quotes “.

For instance,

let someString: String = "Swift is fun" 

Here, “Swift is fun” is a string literal assigned to someString.


Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.

salman khan

Written by worldofitech

Leave a Reply

HTML Headings

HTML Headings

HTML Paragraphs

HTML Paragraphs