Swift Fundamentals for the SQL-Savvy Coder
Swift Basics
Welcome to Swift
Swift is a modern programming language created by Apple. It’s known for being fast, safe, and easy to read. While it’s the primary language for building apps on iOS, macOS, and other Apple platforms, it's versatile enough for other uses, like building web services.
Swift is a powerful and modern programming language used to build applications for Apple’s platforms.
One of Swift's core features is safety. It's designed to help you write code that is less prone to common programming errors. It achieves this in part through a strong type system, which we'll explore shortly.
Storing Data
Programs work by manipulating data. To do that, we need a way to store values and give them names. In Swift, we have two ways to do this: constants and variables.
A constant is a value that cannot be changed once it's set. A variable is a value that can be changed.
We declare constants using the let keyword. Think of these for values that are fixed, like the number of days in a week or a user's date of birth.
let daysInWeek = 7
let appName = "MyApp"
For values that need to change, we use the var keyword. This is perfect for things like a user's score in a game or the current temperature.
var userScore = 0
// The user completes a level, so we update the score.
userScore = 100
Types of Data
Swift is a type-safe language. This means every variable and constant has a specific data type, like a number or a piece of text. Swift uses this information to catch errors before your code even runs. If you try to add a number to a piece of text, Swift will stop you.
Here are the most common basic data types you'll encounter.
Int
noun
Stands for integer. It's used for whole numbers, both positive and negative, without any decimal points.
let userAge: Int = 30
let numberOfLikes: Int = 542
For numbers with a fractional component, we use Double. This is short for "double-precision floating-point number," and it's ideal for values like prices or scientific measurements.
let price: Double = 19.99
let pi: Double = 3.14159
For logical values, we have Bool, which can only be true or false. Booleans are the foundation of decision-making in code.
let isUserLoggedIn: Bool = true
let hasCompletedTutorial: Bool = false
And for text, we use String. A string is a sequence of characters enclosed in double quotes.
let username: String = "JaneDoe"
let greeting: String = "Hello, world!"
You might have noticed the
: Typeannotation in the examples. This is called type annotation, and it explicitly tells Swift the data type. However, Swift also has type inference. If you writelet score = 10, Swift knowsscoreis anIntwithout you having to say so. It's smart enough to figure it out from the value you provide.
Basic Operations
Now that we can store data, let's perform some simple operations. Swift supports standard arithmetic operators you'll recognize from math class.
| Operator | Name | Example |
|---|---|---|
+ | Addition | 5 + 2 evaluates to 7 |
- | Subtraction | 5 - 2 evaluates to 3 |
* | Multiplication | 5 * 2 evaluates to 10 |
/ | Division | 10 / 2 evaluates to 5 |
We can use these operators with variables and constants.
let initialScore = 100
let bonus = 25
let finalScore = initialScore + bonus // finalScore is 125
Strings have their own special use for the + operator: it combines them. This is called concatenation.
let firstName = "Taylor"
let lastName = "Swift"
let fullName = firstName + " " + lastName // fullName is "Taylor Swift"
A more common and readable way to build strings is with string interpolation. You can embed a constant or variable directly inside a string by wrapping it in \().
let name = "Alex"
let age = 32
let introduction = "My name is \(name) and I am \(age) years old."
// introduction is "My name is Alex and I am 32 years old."
These fundamentals are the building blocks for everything else you'll do in Swift.