How-To Library
Copy, paste, and ship
Practical Swift guides with ready-to-run code.
How to Format Numbers
Use NumberFormatter for readable output.
SwiftHow to Use NavigationStack
Build navigation using the modern API.
SwiftHow to Build a List in SwiftUI
Render a list from an array.
SwiftHow to Use ObservableObject
Share state across multiple views.
SwiftHow to Use @Binding
Pass state down and edits back up.
SwiftHow to Use @State in SwiftUI
Store local view state with @State.
SwiftHow to Write a Simple Unit Test
Use XCTest to verify behavior.
SwiftHow to Match with Regex
Use Swift Regex for pattern matching.
SwiftHow to Use Result
Represent success or failure explicitly.
SwiftHow to Create a Property Wrapper
Wrap common logic in a reusable property wrapper.
SwiftHow to Use a Timer
Run repeated work on a schedule.
SwiftHow to Use NotificationCenter
Broadcast and listen for events in your app.
SwiftHow to Read and Write Files
Write a text file to the documents directory.
SwiftHow to Save Data with UserDefaults
Persist small settings with UserDefaults.
SwiftHow to Use @MainActor
Ensure UI updates happen on the main thread.
SwiftHow to Use Actors
Protect mutable state in concurrent code.
SwiftHow to Use async let
Run async tasks in parallel.
SwiftHow to Use Async/Await
Write async code that reads like sync code.
SwiftHow to Make a POST Request with URLSession
Send JSON data in a POST request.
SwiftHow to Make a GET Request with URLSession
Fetch data from a URL in a few lines.
SwiftHow to Decode JSON with Codable
Parse JSON into Swift types.
SwiftHow to Format Dates
Use DateFormatter for readable dates.
SwiftHow to Sort Arrays
Sort arrays using sort and sorted.
SwiftHow to Use map, filter, reduce
Transform collections with functional helpers.
SwiftHow to Use Trailing Closures
Write cleaner call sites with trailing closure syntax.
SwiftHow to Write Closures
Use closures for inline, reusable behavior.
SwiftHow to Use typealias
Improve readability by renaming complex types.
SwiftHow to Write a Generic Function
Make functions reusable across types.
SwiftHow to Use defer in Swift
Guarantee cleanup work at the end of a scope.
SwiftHow to Create Custom Errors
Define clear error types for your domain.
SwiftHow to Handle Errors with do/try/catch
Handle throwing functions safely.
SwiftHow to Use Access Control
Restrict visibility with public, internal, and private.
SwiftHow to Use Static Properties
Store shared values on the type itself.
SwiftHow to Use Lazy Properties
Delay expensive setup until first use.
SwiftHow to Use Property Observers
React to changes with willSet and didSet.
SwiftHow to Use Computed Properties
Derive values without storing them.
SwiftHow to Add Methods with Extensions
Extend existing types without subclassing.
SwiftHow to Add Default Protocol Implementations
Provide shared behavior with protocol extensions.
SwiftHow to Define a Protocol
Use protocols to define shared requirements.
SwiftHow to Use Class Inheritance
Reuse base behavior with subclassing.
SwiftHow to Create a Class
Use classes for reference semantics and shared state.
SwiftHow to Create a Struct
Use structs for lightweight data models.
SwiftHow to Define an Enum
Group related values with enums.
SwiftHow to Use Nil Coalescing
Provide a default value when optional is nil.
SwiftHow to Use guard let for Early Exit
Keep functions readable by handling failures up front.
SwiftHow to Use Optionals Safely
Handle missing values without crashes.
SwiftHow to Use Sets
Track unique values with fast membership checks.
SwiftHow to Use Dictionaries
Store key-value data with fast lookups.
SwiftHow to Work with Arrays
Create, update, and iterate arrays safely.
SwiftHow to Use Tuples for Quick Grouping
Return or group small bits of data without a custom type.
SwiftHow to Use Default Parameters
Provide sensible defaults to keep call sites clean.
SwiftHow to Define and Call Functions
Extract logic into reusable functions.
SwiftHow to Use a While Loop
Repeat work while a condition remains true.
SwiftHow to Write a For-In Loop
Loop through sequences using for-in.
SwiftHow to Use Switch in Swift
Match values safely with exhaustive switch cases.
SwiftHow to Write If-Else Logic
Branch behavior with clear, readable conditions.
SwiftHow to Read User Input from the Console
Use readLine to capture simple input in CLI tools.
SwiftHow to Use String Interpolation
Build readable strings with interpolation and formatting.
SwiftHow to Use Variables and Constants
Prefer constants with let and fall back to var only when needed.
SwiftHow to Print Output in Swift
Use print and debugPrint to inspect values quickly.
Swift