Welcome to the Swift Basics guide! 🚀 This document covers fundamental concepts for beginners learning Swift programming. If you're new to Swift, start here to build a solid foundation.
Table of Contents
Introduction to Swift
Swift is a powerful and intuitive programming language developed by Apple. It's designed for building apps for iOS, macOS, watchOS, and tvOS. 🍎
Swift combines the best features of Objective-C and modern programming languages, making it both safe and expressive. For more details on Swift's history and features, check our Swift Overview.
Variables and Constants
Variables store values that can change, while constants store values that remain the same. 📌
Declaring Variables
var message = "Hello, Swift!"
message = "Welcome to programming!" // Variables can be updated
Declaring Constants
let pi = 3.14159 // Constants cannot be changed
Use var
for mutable data and let
for immutable data. A great example of variables in action is this tutorial.
Functions
Functions are blocks of code that perform a specific task. 🧠
Basic Function Syntax
func greet(name: String) -> String {
return "Hello, $name)! 🌟
}
Function Calls
greet(name: "Alice") // Returns "Hello, Alice)!
Explore more about functions and their advanced uses in this section.
Control Flow
Control flow refers to the order in which code is executed. 🔄
Conditional Statements
if age >= 18 {
print("You are an adult!") 🎉
} else {
print("You are a minor.") 😕
}
Loops
for i in 1...5 {
print(i) 📈
}
Learn more about control flow in this guide.
Collections
Collections are used to store multiple items in a single place. 🧾
Arrays
var fruits = ["Apple", "Banana", "Cherry"] 🍎🍌🍒
Dictionaries
let fruitColors = ["Apple": "Red", "Banana": "Yellow"] 🍌
Check out collections in depth for more information.
Further Reading
For visual learners, explore Swift Diagrams to see data structures and flowcharts. 📊