In Swift, variables and constants are fundamental concepts that allow you to store and manipulate data. Understanding how to use them effectively is crucial for any Swift programmer. This section will cover the following topics:

  1. Introduction to Variables and Constants
  2. Declaring Variables
  3. Declaring Constants
  4. Type Inference and Type Annotation
  5. Mutability
  6. Practical Examples
  7. Exercises

  1. Introduction to Variables and Constants

Variables and constants in Swift are used to store values. The key difference between them is that variables can change their values after they are set, while constants cannot.

  • Variables: Use the var keyword to declare a variable.
  • Constants: Use the let keyword to declare a constant.

  1. Declaring Variables

To declare a variable, use the var keyword followed by the variable name and an optional type annotation.

var variableName: DataType = initialValue

Example:

var age: Int = 25
var name: String = "John"

In the above example:

  • age is a variable of type Int with an initial value of 25.
  • name is a variable of type String with an initial value of "John".

  1. Declaring Constants

To declare a constant, use the let keyword followed by the constant name and an optional type annotation.

let constantName: DataType = initialValue

Example:

let pi: Double = 3.14159
let birthYear: Int = 1990

In the above example:

  • pi is a constant of type Double with an initial value of 3.14159.
  • birthYear is a constant of type Int with an initial value of 1990.

  1. Type Inference and Type Annotation

Swift is a type-safe language, which means it performs type checks when compiling your code. Swift can infer the type of a variable or constant based on the initial value you provide.

Type Inference:

var city = "New York" // Swift infers that city is of type String
let temperature = 72.5 // Swift infers that temperature is of type Double

Type Annotation:

You can explicitly specify the type of a variable or constant using a type annotation.

var city: String = "New York"
let temperature: Double = 72.5

  1. Mutability

  • Variables: The value of a variable can be changed after it is set.
var score = 10
score = 20 // The value of score is now 20
  • Constants: The value of a constant cannot be changed once it is set.
let maxScore = 100
// maxScore = 200 // This will cause a compile-time error

  1. Practical Examples

Example 1: Using Variables

var currentYear = 2023
currentYear = 2024 // The value of currentYear is now 2024
print("The current year is \(currentYear)")

Example 2: Using Constants

let companyName = "Apple Inc."
// companyName = "Google" // This will cause a compile-time error
print("The company name is \(companyName)")

  1. Exercises

Exercise 1: Declaring Variables and Constants

  1. Declare a variable called height of type Double and set it to 5.9.
  2. Declare a constant called maxSpeed of type Int and set it to 120.
  3. Change the value of the height variable to 6.1.
  4. Try to change the value of the maxSpeed constant and observe the error.

Solution:

// 1. Declare a variable called height of type Double and set it to 5.9
var height: Double = 5.9

// 2. Declare a constant called maxSpeed of type Int and set it to 120
let maxSpeed: Int = 120

// 3. Change the value of the height variable to 6.1
height = 6.1

// 4. Try to change the value of the maxSpeed constant and observe the error
// maxSpeed = 130 // This will cause a compile-time error

Exercise 2: Type Inference and Type Annotation

  1. Declare a variable called username and set it to "Alice" without specifying the type.
  2. Declare a constant called pi and set it to 3.14 without specifying the type.
  3. Declare a variable called isLoggedIn of type Bool and set it to true.

Solution:

// 1. Declare a variable called username and set it to "Alice" without specifying the type
var username = "Alice" // Swift infers that username is of type String

// 2. Declare a constant called pi and set it to 3.14 without specifying the type
let pi = 3.14 // Swift infers that pi is of type Double

// 3. Declare a variable called isLoggedIn of type Bool and set it to true
var isLoggedIn: Bool = true

Conclusion

In this section, you learned about variables and constants in Swift, including how to declare them, the difference between them, and how to use type inference and type annotation. You also practiced with some exercises to reinforce your understanding. In the next section, we will dive into data types and operators in Swift.

© Copyright 2024. All rights reserved