Mastering iOS Development
Swift Programming Basics
Your First Steps in Swift
Swift is the modern programming language created by Apple. It’s designed to be safe, fast, and easy to read. Think of it as the foundation for building apps on any Apple platform, from your iPhone to your Mac.
One of Swift's best features is its clean syntax. The code you write is often as readable as plain English, which makes it easier to learn and maintain. Let's start with the most basic building block: storing information.
Storing Information
In programming, you need places to store data, like a user's name or the score in a game. Swift gives you two ways to do this: variables and constants.
A variable, declared with
var, is a value that can change. A constant, declared withlet, is a value that you set once and can never change.
It's a good practice to use constants (let) whenever you can. This makes your code safer by preventing you from accidentally changing a value that shouldn't be changed.
let maxScore = 100 // A constant, can't be changed
var currentScore = 85 // A variable, can be updated
currentScore = 90 // This is okay
// maxScore = 110 // This would cause an error!
Every piece of data has a type. Swift is smart and can usually figure out the type on its own, a feature called type inference. Here are a few fundamental types:
| Type | What it stores | Example |
|---|---|---|
String | Text | "Hello, world!" |
Int | Whole numbers | 42 |
Double | Numbers with decimal points | 3.14159 |
Bool | A true or false value | true |
When you declare a variable or constant, Swift automatically assigns its type.
let name = "Alice" // Swift knows this is a String
let age = 30 // Swift knows this is an Int
var isStudent = true // Swift knows this is a Bool
Making Decisions
Programs often need to make decisions. Does the user have enough points to level up? Is their password correct? You handle these situations with if and else statements.
An if statement checks if a condition is true. If it is, it runs a block of code. You can add an else block to run different code if the condition is false.
var temperature = 25
if temperature > 20 {
print("It's a warm day.")
} else {
print("It might be a bit chilly.")
}
Sometimes you need to repeat an action. To do this, you use loops. The most common loop in Swift is the for-in loop, which is perfect for running a piece of code for every item in a collection, like an array of names.
let names = ["Anna", "Brian", "Charles"]
for name in names {
print("Hello, \(name)!")
}
Packaging Code in Functions
As your programs get bigger, you don't want to repeat the same lines of code over and over. Functions let you package up a piece of work into a reusable block. You give it a name, and then you can run it whenever you need it just by calling that name.
You define a function using the func keyword. You can also define inputs, called parameters, and an output, called a return value.
// This function takes two integers and returns their sum.
func add(a: Int, b: Int) -> Int {
return a + b
}
Once defined, you can call the function like this:
let sum = add(a: 5, b: 3) // sum is now 8
print(sum)
Blueprints for Objects
Object-Oriented Programming (OOP) is a way of thinking about code that models real-world things. In Swift, you create these models using class or struct.
Think of a class as a blueprint. For example, you could have a Dog blueprint. It would define the properties all dogs have (like a name and an age) and the things all dogs can do (like bark).
class Dog {
var name: String
var age: Int
// This is an initializer, it sets up the object
init(name: String, age: Int) {
self.name = name
self.age = age
}
// This is a method (a function inside a class)
func bark() {
print("Woof!")
}
}
The blueprint itself isn't a dog. To get an actual dog, you create an instance of the class. This is called creating an object.
let myDog = Dog(name: "Fido", age: 3)
print(myDog.name) // Prints "Fido"
myDog.bark() // Prints "Woof!"
These fundamentals are the core of writing Swift. By combining variables, control flow, functions, and classes, you can start building powerful and complex applications.
What is the primary difference between declaring a value with let versus var in Swift?
True or False: In Swift, if you don't specify a data type for a variable, the program will produce an error.
You now have a solid foundation in the basics of Swift. These concepts are the building blocks you'll use every day as an iOS developer.