Skip to main content
freelanceshack.com

Back to all posts

How to Declare Variables/Constants In Swift?

Published on
4 min read
How to Declare Variables/Constants In Swift? image

Best Programming Guides to Buy in October 2025

1 Python Crash Course, 3rd Edition: A Hands-On, Project-Based Introduction to Programming

Python Crash Course, 3rd Edition: A Hands-On, Project-Based Introduction to Programming

BUY & SAVE
$27.53 $49.99
Save 45%
Python Crash Course, 3rd Edition: A Hands-On, Project-Based Introduction to Programming
2 Beginner's Step-by-Step Coding Course: Learn Computer Programming the Easy Way (DK Complete Courses)

Beginner's Step-by-Step Coding Course: Learn Computer Programming the Easy Way (DK Complete Courses)

BUY & SAVE
$16.01 $30.00
Save 47%
Beginner's Step-by-Step Coding Course: Learn Computer Programming the Easy Way (DK Complete Courses)
3 C Programming Language, 2nd Edition

C Programming Language, 2nd Edition

BUY & SAVE
$60.30 $69.99
Save 14%
C Programming Language, 2nd Edition
4 Everything You Need to Ace Computer Science and Coding in One Big Fat Notebook: The Complete Middle School Study Guide (Big Fat Notebooks)

Everything You Need to Ace Computer Science and Coding in One Big Fat Notebook: The Complete Middle School Study Guide (Big Fat Notebooks)

BUY & SAVE
$10.98 $16.99
Save 35%
Everything You Need to Ace Computer Science and Coding in One Big Fat Notebook: The Complete Middle School Study Guide (Big Fat Notebooks)
5 Code: The Hidden Language of Computer Hardware and Software

Code: The Hidden Language of Computer Hardware and Software

BUY & SAVE
$21.39 $39.99
Save 47%
Code: The Hidden Language of Computer Hardware and Software
6 The Pragmatic Programmer: Your Journey To Mastery, 20th Anniversary Edition (2nd Edition)

The Pragmatic Programmer: Your Journey To Mastery, 20th Anniversary Edition (2nd Edition)

BUY & SAVE
$47.04 $54.99
Save 14%
The Pragmatic Programmer: Your Journey To Mastery, 20th Anniversary Edition (2nd Edition)
+
ONE MORE?

In Swift, variables and constants are declared using the var and let keywords respectively.

To declare a variable, you use the var keyword followed by the name of the variable, a colon, and the type of the variable. For example:

var age: Int

In the above example, a variable named age of type Int (integer) is declared. It can be assigned any integer value.

To declare a constant, you use the let keyword followed by the name of the constant, a colon, and the type of the constant. For example:

let pi: Double

In the above example, a constant named pi of type Double is declared. Unlike variables, constants cannot be reassigned once a value is assigned to them.

You can also provide an initial value while declaring variables or constants. For example:

var name: String = "John" let gravity: Float = 9.8

In the above examples, the variable name is assigned an initial value of "John" and the constant gravity is assigned an initial value of 9.8.

If you don't provide an initial value, you can specify the type explicitly or rely on type inference to determine the type from the assigned value. For example:

var score: Int

In this case, the type of the score variable is not specified explicitly, so it will be inferred as Int based on the fact that it is an integer type.

It's important to note that Swift is a statically-typed language, which means that once a variable or constant is declared with a specific type, it cannot be changed to a different type in its lifetime.

How to declare and initialize an array in Swift?

To declare and initialize an array in Swift, you can use the following syntax:

  1. Declare and initialize an empty array:

var arrayName = [Type]()

For example:

var numbers = [Int]()

  1. Declare and initialize an array with values:

var arrayName = [value1, value2, value3, ...]

For example:

var fruits = ["apple", "banana", "orange"]

  1. Declare and initialize an array with a specified size:

var arrayName = [Type](repeating: initialValue, count: size)

For example:

var zeros = [Int](repeating: 0, count: 5)

  1. Alternative syntax to declare and initialize an array with values:

var arrayName: [Type] = [value1, value2, value3, ...]

For example:

var numbers: [Int] = [1, 2, 3, 4, 5]

Note: The type of the array elements should be consistent.

What is the difference between static and instance variables in Swift?

In Swift, static variables are associated with the class itself, rather than with any specific instance of the class. This means that all instances of the class share the same static variable. It is declared using the static keyword.

On the other hand, instance variables are specific to each instance of the class. Each instance of the class has its own unique copy of the instance variable. It is declared as a property within the class but not with the static keyword.

To summarize:

  • Static variables are shared across all instances of the class.
  • Instance variables are unique to each instance of the class.

How to declare variables with different access levels in Swift?

In Swift, you can declare variables with different access levels by using the public, internal, fileprivate, or private keywords.

  1. public: A variable declared with public access level can be accessed from any source file within the module or from another module that imports the current module.

public var publicVariable = 10

  1. internal: This is the default access level in Swift, and a variable declared with internal access level can be accessed within any source file within the module.

internal var internalVariable = 20

  1. fileprivate: A variable declared with fileprivate access level can be accessed only within the same source file that it is declared in.

fileprivate var fileprivateVariable = 30

  1. private: A variable declared with private access level can only be accessed within the enclosing declaration, such as a function or a type.

private var privateVariable = 40

It is important to note that the access levels are progressive, meaning that private is the most restrictive and public is the least restrictive.

What is the purpose of typealias in Swift?

The purpose of typealias in Swift is to provide an alternative name for an existing type. It allows developers to give new names to types, making the code more expressive and readable. typealias is particularly useful when working with complex or long type names, or when developers want to create aliases for generic types to make them more specific or descriptive. It simplifies the code and enhances its overall clarity and maintainability.