Variables, constants, and data types
Section 1.3: Swift: Variables, Constants, and Data Types
Welcome to the world of data in Swift! In this section, we'll dive into the fundamental building blocks of Swift programming: variables, constants, and data types. These concepts are crucial for storing, managing, and manipulating information within your apps.
Variables: The Changeable Containers
Think of variables as labeled boxes where you can store different types of data. The data within a variable can be changed throughout your program's execution.
Declaring Variables:
To create a variable, you use the var keyword followed by the variable name and its type. Here's the basic syntax:
Swift
var variableName: DataType = initialValue
Example:
Swift
var greeting: String = "Hello, world!"
var score: Int = 95
var price: Double = 19.99
Changing Variable Values:
You can easily change the value of a variable by assigning a new value to it.
Swift
greeting = "Welcome to Swift!"
score = 88
price = 24.95
Constants: The Unchanging Guardians
Constants are similar to variables, but their values cannot be changed once they are set. Use constants to store values that remain fixed throughout your program's execution.
Declaring Constants:
To create a constant, you use the let keyword followed by the constant name and its type.
Swift
let constantName: DataType = initialValue
Example:
Swift
let pi: Double = 3.14159
let companyName: String = "Apple Inc."
Data Types: Classifying Information
Every value in Swift has a specific data type, which determines the kind of data it can hold and the operations you can perform on it. Swift is a strongly typed language, meaning you need to declare the data type of each variable and constant.
Common Data Types:
Int: Represents whole numbers (e.g., 10, -5, 0).
Double: Represents floating-point numbers (e.g., 3.14, -0.5).
String: Represents a sequence of characters (e.g., "Hello, world!").
Bool: Represents a Boolean value (true or false).
Type Inference: Swift's Smart Deduction
Swift is smart enough to infer the data type of a variable or constant based on the initial value you assign to it. This means you don't always have to explicitly specify the type.
Example:
Swift
var age = 25Â // Swift infers 'age' as Int
let message = "Hello!" // Swift infers 'message' as String
Type Annotation: Explicitly Stating the Type
While type inference is convenient, sometimes you might want to explicitly declare the data type for clarity or to catch potential errors.
Example:
Swift
var studentName: String = "John Doe"
let maximumAttempts: Int = 3
Key Takeaways:
Variables (var) store data that can change.
Constants (let) store data that remains fixed.
Data types classify information (e.g., Int, Double, String, Bool).
Type inference allows Swift to deduce the data type automatically.
Type annotation lets you explicitly specify the data type.
Understanding variables, constants, and data types is fundamental for building any Swift application. These concepts lay the groundwork for storing and manipulating information in a structured and efficient manner.