1. Swift Variables
In programming, a variable is a container (storage area) to hold data. For example,
var num = 10
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:Stringvar id: Int
- 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
var siteName: String siteName = "programiz.com" print(siteName)
You can also assign a variable directly without the type annotation as:
var siteName = "programiz.com" print(siteName) // programiz.com
Here, the compiler automatically figures out that siteName is a variable of the
Change Value of a Variable
You can change the value of an existing variable. Hence, the name variable. For example,
var siteName = "programiz.com" // assigning a new value to siteName siteName = "apple.com" print(siteName)
Here, the value of siteName is changed from "programiz.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
class, etc. as variable names.
- 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,
let x = 5 x = 10 // Error print(x)
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,
let siteName: String print(siteName)
main.swift:4:7: error: constant 'siteName' used before being initialized
- If you are sure that the value of a variable won't change throughout the program, it's recommended to use
- 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.
let siteName = "Apple.com"
In the above expression, siteName is a variable, and
"Apple.com" is a literal.
Integer literals are those that do not have a fractional or an exponential part.
There are four types of integer literals in Swift:
|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 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.
There are two boolean literals:
let pass: Bool = true
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"
S is a character literal assigned to someCharacter.
Similarly, String literals are sequences of characters enclosed in double quotes
let someString: String = "Swift is fun"
Here, "Swift is fun" is a string literal assigned to someString.