1. Swift Variables
In programming, a variable is a container (storage area) to hold data. For example,
var num = 10
Here, num is a variable storing the value 10.
Declare Variables in Swift
In Swift, we use the var keyword to declare variables. For example,
var siteName:String
var id: Int
Here,
- siteName is a variable of type String. Meaning, it can only store textual values.
- id is a variable of
Inttype. Meaning, it can only store integer values.
Note: In Swift, we cannot change the type of a variable once it's declared.
Assign Values to Variables
You can assign values to variables using the = operator.
Output
nodmek.com
You can also assign a variable directly without the type annotation as:
Here, the compiler automatically figures out that siteName is a variable of the String type.
Change Value of a Variable
You can change the value of an existing variable. Hence, the name variable. For example,
Output
apple.com
Here, the value of siteName is changed from "nodmek.com" to "apple.com".
Rules for naming Swift Variables
The rules for naming variables are:
- Variables names must start with either a letter, an underscore
_, or the dollar sign$. For example,// valid var a = "hello" var _a = "hello" var $a = "hello" - Variable names cannot start with numbers. For example,
// invalid var 1a = "hello" // throws error - Swift is case-sensitive. So A and a are different variables. For example,
var A = 5 var a = 55 print(A) // 5 print(a) // 55 - Avoid using Swift keywords like
var,String,class, etc. as variable names.
Notes:
- It's a good practice to give a descriptive variable name. For example, numberofApples is a better variable name than a, apple, or n.
- In Swift, variable names are generally written in camelCase if they have multiple words. For example, myVariable, addTwoNums, etc.
2. Swift Constants
A constant is a special type of variable whose value cannot be changed. For example,
let a = 5
Here, after a is initialized to 5, we cannot change its value.
Declare Constants in Swift
In Swift, we use the let keyword to declare constants. The value of a constant cannot be changed. For example,
Output
main.swift:4:1: error: cannot assign to value: 'x' is a 'let' constant
Also, you cannot declare a constant without initializing it. For example,
Output
main.swift:4:7: error: constant 'siteName' used before being initialized
Notes:
- If you are sure that the value of a variable won't change throughout the program, it's recommended to use
let. - The rules for naming variables also apply to constants.
3. Swift Literals
Literals are representations of fixed values in a program. They can be numbers, characters, or strings, etc. For example, "Hello, World!", 12, 23.0, "C", etc.
Literals are often used to assign values to variables or constants.
For example:
let siteName = "Apple.com"
In the above expression, siteName is a variable, and "Apple.com" is a literal.
Integer Literals
Integer literals are those that do not have a fractional or an exponential part.
There are four types of integer literals in Swift:
| Type | Example | Remarks |
|---|---|---|
| Decimal | 5, 10, -68 | Regular numbers. |
| Binary | 0b101, 0b11 | Start with 0b. |
| Octal | 0o13 | Start with 0o. |
| Hexadecimal | 0x13 | Start with 0x. |
Floating-point Literals
Floating-point literals are numeric literals that have floating decimal points. For example,
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 example,
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 example,
let someCharacter: Character = "S"
Here, S is a character literal assigned to someCharacter.
Similarly, String literals are sequences of characters enclosed in double quotes ".
For example,
let someString: String = "Swift is fun"
Here, "Swift is fun" is a string literal assigned to someString.