Swift iOS Development Journey
Swift Basics
Your First Look at Swift
Think of a programming language as a set of instructions a computer can understand. Swift is a modern, powerful language created by Apple. It's designed to be safe, fast, and easy to read. Let's start with the most traditional first program.
print("Hello, world!")
That's it. The print() command, or function, simply displays whatever is inside the parentheses on the screen. The text is wrapped in double quotes, which tells Swift that it's a string of characters.
Syntax
noun
The set of rules that defines the combinations of symbols that are considered to be correctly structured statements or expressions in a language.
Storing Information
In programming, you constantly need to store and manage information. Swift gives you two ways to do this: variables and constants.
A constant is a storage spot for a value that won't change. Once you set it, it's fixed. You declare a constant using the let keyword.
A variable stores a value that can change. You declare a variable using the var keyword.
Use
letwhen you know a value won't change. It makes your code safer and easier to understand. If you might need to change the value later, usevar.
Let's see them in action. We'll create a constant for a user's name and a variable for their score, which might change.
// A constant for a value that won't change
let userName = "Alex"
// A variable for a score that might change
var userScore = 100
// You can change the variable's value later
userScore = 120 // This is fine
// But you can't change the constant
// userName = "Jamie" // This would cause an error!
Understanding Data Types
Every variable and constant in Swift has a type. The type tells Swift what kind of data it's holding. Swift is smart and can often figure out the type on its own, a feature called type inference. Here are the most common types you'll use:
String: A piece of text, like"Hello".Int: A whole number without a decimal point, like10or-5.Double: A number with a decimal point, like3.14or99.9.Bool: A value that can only betrueorfalse.
let welcomeMessage: String = "Welcome!"
let userAge: Int = 25
let accountBalance: Double = 150.75
let isLoggedIn: Bool = true
Notice how we can explicitly state the type after the name, using a colon. While Swift can infer these types, being explicit can make your code clearer. For example, Swift infers a number with a decimal to be a Double by default.
Making Decisions and Repeating Actions
Your programs will often need to make decisions or perform actions repeatedly. This is handled by control flow structures.
To make a decision, you use an
ifstatement. It checks if a condition is true and runs a block of code if it is. You can also provide anelseblock to run if the condition is false.
var temperature = 25
if temperature > 20 {
print("It's a warm day.")
} else {
print("It's a bit chilly.")
}
To repeat an action, you can use a loop. A for-in loop is great for running a piece of code for each item in a collection, like a range of numbers.
// This loop will run 5 times
for number in 1...5 {
print("This is number \(number)")
}
// Output:
// This is number 1
// This is number 2
// This is number 3
// This is number 4
// This is number 5
The 1...5 creates a range of numbers from 1 to 5, inclusive. The code inside the curly braces {} runs for each number in that range. The \(number) syntax is called string interpolation, and it's how you place the value of a variable or constant directly inside a string.
You've just learned the absolute fundamentals of Swift. Let's see what you remember.
In Swift, which keyword is used to declare a constant, a value that cannot be changed once set?
What is the most appropriate data type in Swift for storing a whole number like 42?