Native iOS Development Fundamentals
Introduction to Swift
Meet Swift
Swift is the programming language used to build apps for all of Apple's platforms, from your iPhone and Apple Watch to your Mac. It was created by Apple in 2014 with a clear goal: to be a language that's easy to learn but powerful enough for complex tasks. It's designed to be safe, preventing common programming errors, and fast, so your apps run smoothly.
Swift is a general-purpose programming language designed by Apple in 2014.
Think of it as the blueprint for your app. Every button you tap, every screen you swipe, is brought to life with code. Learning Swift is the first and most important step to creating your own iOS applications.
Storing Information
In programming, we need places to store information, like a user's name or the current score in a game. Swift gives us two ways to do this: variables and constants.
A constant is a value that, once set, cannot be changed. You declare it using the keyword let. This is perfect for things that don't change, like the value of pi or a person's date of birth.
A variable, on the other hand, can be changed after it's set. You declare it with var. This is for values that need to be updated, like a player's score or the temperature.
let bestScore = 100 // A constant that won't change
var currentScore = 85 // A variable that can be updated
currentScore = 90 // This is fine
// bestScore = 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. But it's good to know the common ones:
| Data Type | Description | Example |
|---|---|---|
String | A piece of text | "Hello, world!" |
Int | A whole number | 42 |
Double | A number with a decimal | 3.14159 |
Bool | A true or false value | true |
Sometimes you might want to be explicit about the type, which you can do with a colon.
let name: String = "Jordan"
var age: Int = 30
Making Decisions
Apps constantly make decisions. If you enter the right password, you log in. If not, you get an error. This logic is handled with control flow statements.
The most common way to make a decision is with an if statement. It checks if a condition is true and runs a block of code if it is. You can add else if to check other conditions, and else as a fallback for when none of the conditions are met.
let temperature = 25 // in Celsius
if temperature > 30 {
print("It's a hot day.")
} else if temperature < 10 {
print("It's cold, bring a jacket!")
} else {
print("The weather is just right.")
}
What if you need to repeat an action? That's where loops come in. A for-in loop lets you run a block of code for each item in a sequence, like a range of numbers.
// This loop will run 3 times
for number in 1...3 {
print("Hello, number \(number)!")
}
// Output:
// Hello, number 1!
// Hello, number 2!
// Hello, number 3!
Packaging Code with Functions
As your app grows, you'll find yourself writing the same lines of code over and over. Functions let you package up a piece of code and give it a name. Then, you can run that code just by calling its name. This keeps your code organized and reusable.
Think of it like a recipe. The function is the recipe, and calling the function is like cooking it.
// Define a simple function
func showWelcomeMessage() {
print("Welcome to our app!")
}
// Call the function to run its code
showWelcomeMessage()
Functions can also take in data, called parameters, and return data as a result. This makes them much more flexible. For example, we can create a function that takes a person's name as input and returns a personalized greeting.
// A function with a parameter (name) and a return value (String)
func createGreeting(for name: String) -> String {
let greeting = "Hello, " + name + "!"
return greeting
}
// Call the function and store its return value
let personalizedMessage = createGreeting(for: "Sam")
print(personalizedMessage) // Prints: Hello, Sam!
With these building blocks—variables, control flow, and functions—you have the foundation you need to start writing real code in Swift.
Time to check what you've learned.
In Swift, if you need to store a value that will not change throughout the program's execution, such as a user's date of birth, which keyword should you use to declare it?
True or False: In Swift, you must always explicitly declare the type of a variable or constant (e.g., var name: String).
Understanding these core concepts is the first step on your journey to becoming an iOS developer. From here, you can explore more complex ideas and start building your own applications.