Swift
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Optionals
-
Section 4: Object-Oriented and Value Types
-
Section 5: Memory Management
-
Section 6: Generics and Error Handling
-
Section 7: Concurrency
-
Section 8: Working with Collections
-
Section 9: Codable and Data Handling
-
Section 10: Protocol-Oriented Programming
-
Section 11: Testing and Tooling
-
Section 12: Practical Projects
-
Section 13: Interview Practice
-
Section 14: More Practice Exercises
-
Section 15: More Standard Library
-
Section 16: Advanced Concurrency
-
Section 17: Foundation Framework Deep Dive
-
Section 18: URLSession and Networking Deep Dive
-
Section 19: Combine Framework
-
Section 20: SwiftUI Fundamentals for Swift Developers
-
Section 21: Server-Side Swift with Vapor
-
Section 22: Swift Package Manager Deep Dive
-
Section 23: Swift Concurrency Deep Dive
-
Section 24: More Language Features
-
Section 25: Error Handling Deep Dive
-
Section 26: Testing Deep Dive
-
Section 27: Data Structures and Algorithms in Swift
-
Section 28: More Practice Exercises
-
Section 29: More Interview Practice
-
Section 30: Swift Macros (Swift 5.9+)
-
Section 31: Property Wrappers Ecosystem
-
Section 32: Swift Interop Deep Dive
-
Section 33: iOS App Architecture Patterns
-
Section 34: Performance and Debugging
-
Section 35: App Distribution and CI/CD
-
Section 36: More Practical Projects
-
Section 37: SwiftData and Persistence
-
Section 38: More Design Patterns
-
Section 39: More Review and Practice
-
Section 40: More Foundation Deep Dive
-
Section 41: Advanced Collections in Swift
-
Section 42: Advanced Generics Practice
-
Section 43: UIKit for Legacy and Hybrid Apps
-
Section 44: watchOS and visionOS Development Basics
-
Section 45: More Networking Patterns
-
Section 46: More Testing Practice
-
Section 47: Accessibility in Swift Apps
-
Section 48: Localization
-
Section 49: More Practical Projects Round 2
-
Section 50: Swift Charts Framework
-
Section 51: More Interview and Algorithm Practice
-
Section 52: Final Practice and Mastery
-
Section 53: Swift Compiler and Build System
-
Section 54: More Concurrency Practice
-
Section 55: App Store Guidelines and Review
-
Section 56: More Design and Architecture
9: String Interpolation and Manipulation
How do I plug variables directly into a string without using a bunch of plus signs?
If you're coming from other languages, you might be tempted to use the + operator to glue strings together. In Swift, we call that concatenation, and honestly, it gets ugly fast once you have more than two variables. Instead, we use string interpolation.
You just wrap your variable or expression in \( ) inside the double quotes. Swift handles the conversion to a string for you automatically. I use this constantly for things like debugging logs or dynamic UI labels.
let username = "CyberKnight_99"
let level = 42
let health = 85.5
// Instead of "User: " + username + " Level: " + String(level)...
let statusReport = "Player: \(username) | Level: \(level) | HP: \(health)%"
print(statusReport)
// Output: Player: CyberKnight_99 | Level: 42 | HP: 85.5%
One little trick: you can actually put basic math or logic inside those parentheses. Just don't go overboard, or your code becomes a nightmare to read. Keep it simple.
What's the best way to clean up messy user input, like accidental spaces or weird casing?
Users are unpredictable. They'll add a trailing space to their email address or accidentally hit Caps Lock. If you try to compare "User@Email.com" with "user@email.com", Swift will tell you they aren't the same because strings are case-sensitive.
To fix this, I usually chain a few manipulation methods together. trimmingCharacters(in:) is your best friend for removing that invisible whitespace at the start or end, and lowercased() ensures your comparisons are consistent.
let rawInput = " SwiftLearner@Example.com "
// Chain them together for a clean result
let sanitizedEmail = rawInput
.trimmingCharacters(in: .whitespacesAndNewlines)
.lowercased()
print("'\(sanitizedEmail)'")
// Output: 'swiftlearner@example.com'
Can I easily swap out a specific word or phrase within a larger string?
Sometimes you have a template string—maybe a notification message—and you need to swap out a placeholder for real data. While interpolation is great for building a string from scratch, replacingOccurrences(of:with:) is the way to go when you're modifying an existing string.
I find this particularly useful when handling localized strings or templates coming from a server.
var welcomeMessage = "Hello [Name], you have [Count] new notifications!"
let personalizedMessage = welcomeMessage
.replacingOccurrences(of: "[Name]", with: "Sarah")
.replacingOccurrences(of: "[Count]", with: "7")
print(personalizedMessage)
// Output: Hello Sarah, you have 7 new notifications!
Just a heads-up: replacingOccurrences returns a new string. It doesn't change the original one in place unless you assign it back to a var.
📋 Practical Task
Exercise: The Social Media Bio Formatter
You are building a profile editor. You've received a "raw" bio from a user that is messy and contains a placeholder tag. Your goal is to clean it up and format it for the public profile.
The Requirements:
- Start with this variable:
let rawBio = " I love coding in Swift and living in [City]! " - Remove the leading and trailing whitespace.
- Replace the
[City]placeholder with your actual city (or any city of your choice). - Convert the entire string to uppercase to make it a "shouting" bio.
- Finally, use string interpolation to print the result inside this sentence:
"User Bio: [Your cleaned string here]"
There are no comments for now.