ios-swift
/

Swift Variables & Constants – Mutability and Type Safety

Last Sync: Today

On this page

4
0%
5 min read
Remaining
5 minleft

Click any section to jump — progress syncs automatically

ios-swift

Swift Variables & Constants – Mutability and Type Safety

Constants and Variables

Every value in Swift is either a constant or a variable. As a Lead Engineer, you should default to using constants (let) unless you are certain the value must change during the program's execution. This 'Immutable by Default' approach reduces side effects and allows the Swift compiler to perform aggressive performance optimizations.

  • let (Constant): The value is set once and cannot be changed. Similar to 'final' in Dart.
  • var (Variable): The value can be updated multiple times as needed.
SWIFTRead-only
1
let platformName = "Revochamp" // Immutable
var currentVersion = 1.0       // Mutable

currentVersion = 1.1           // This works
// platformName = "Revo"       // This would cause a compiler error

  1. Type Inference vs. Type Annotation

Swift is a Type-Safe language, but it is also smart. It uses Type Inference to automatically determine the data type based on the initial value you provide. However, you can use Type Annotation when you want to be explicit or when declaring a variable without an initial value.

SWIFTRead-only
1
// Type Inference (Compiler knows this is a String)
let developerName = "Kishore"

// Type Annotation (Explicitly defining the type)
var projectBudget: Double = 5000.0
var isAIGenerated: Bool

  1. Basic Data Types

Swift provides a set of standard types for common data. Note that Swift types are capitalized (PascalCase), similar to Dart objects.

TypeDescriptionExample
IntWhole numbers (positive/negative)42
Double64-bit floating-point numbers3.14159
Float32-bit floating-point numbers3.14
StringTextual data"Flutter to Swift"
BoolLogical true or falsetrue

  1. String Interpolation

In Swift, you insert variables into strings using the \() syntax. This is the direct equivalent of Dart's ${} syntax.

SWIFTRead-only
1
let count = 5
let message = "Your project has \(count) widgets generated."
print(message)

Test Your Knowledge

Q1
of 3

Which keyword is used to declare a value that will not change?

A
var
B
const
C
let
D
final
Q2
of 3

What is the process called when the Swift compiler automatically determines a variable's data type?

A
Type Casting
B
Type Inference
C
Type Annotation
D
Type Checking
Q3
of 3

Which of these is the correct way to perform string interpolation in Swift?

A
"Value is $value"
B
"Value is {value}"
C
"Value is \(value)"
D
"Value is " + value

Frequently Asked Questions

When should I use Float vs. Double?

In 99% of iOS development, you should use 'Double'. It has higher precision (15 decimal digits vs 6 for Float) and is the default type inferred by the Swift compiler for decimal numbers.

Can I change the type of a variable after declaration?

No. Once a variable is declared as a specific type (either via inference or annotation), its type is fixed for its entire lifetime. You would need to create a new variable or cast the value to a different type.

Does Swift have a 'dynamic' type like Dart?

Swift has 'Any' and 'AnyObject' which can hold any type of value, but their use is generally discouraged in favor of strong typing and Generics to maintain type safety and performance.

Previous

swift syntax

Next

swift control flow

Related Content

Need help?

Explore our comprehensive docs or start a chat with our tech experts.