This post is about creating a confetti animation in SwiftUI with a customizable duration. I learned the technique from this Patreon tutorial.
In the original animation, the confetti pieces would change color while falling, which I considered a bug. I’ve since fixed it. Another optimization I considered was adding a fade-out effect to make the disappearance of the confetti smoother, but the result wasn’t satisfactory, so I have not included that change.
Let’s break down how to build this.
Step 1: Animate a Single Piece of Confetti
First, we’ll create the animation for a single piece of confetti. The code and the result are below:
This line of code is the key to fixing the color-changing issue:
@State private var color: Color = [.orange, .green, .blue, .red, .yellow].randomElement() ?? .green
The color used to change because the original code would assign a new random color during the animation process. By using @State to store the color, we ensure its value is preserved for the view’s lifetime, preventing the color from changing unexpectedly.
Step 2: Create the Confetti Container
Next, we’ll create a container to hold 50 confetti pieces and manage their positioning.
Sometimes, you have to explore different paths to find where you truly belong. For a developer, that path is often paved with different languages, frameworks, and platforms. My own journey has taken me through the worlds of backend development, cross-platform apps with React Native and Flutter, and of course, native iOS. And after seeing what each has to offer, I’ve come to a clear and exciting realization: my true passion lies in building for native iOS.
A Journey Across the Stack
Every technology I’ve worked with has taught me something invaluable.
Building backend systems gave me a deep appreciation for data architecture, APIs, and the logic that powers applications from behind the scenes.
Working with React Native and Flutter was a fantastic lesson in efficiency and the challenge of creating a consistent experience across different operating systems. The promise of “write once, run anywhere” is compelling, and I learned a great deal about managing a single codebase for multiple targets.
This broad perspective is something I wouldn’t trade. It gave me a holistic view of how a product comes to life, from the database all the way to the user’s screen. But it also created a point of comparison that continually highlighted what makes native development, and specifically iOS, so special to me.
The Pull of Native iOS
There’s a certain elegance and satisfaction in iOS development that I kept coming back to. The seamless integration between the Swift language, powerful frameworks like SwiftUI, and the hardware itself allows for a level of polish and performance that is simply a joy to create. The pursuit of the perfect animation, the crispness of a native UI component, and the satisfaction of building something that feels completely at home on the device—that’s what excites me as a developer.
After reflecting on this, I felt a renewed surge of energy and inspiration. To channel it into something tangible, I decided to build a small, focused project that captures the kind of delightful interaction I love: a simple “lights-out” animation.
Project: The “Lights-Out” Animation
I wanted to create more than just a toggle. I wanted to build an experience. The idea was to mimic the satisfying, physical act of pulling a cord to turn a light on and off, complete with animated light beams, a draggable cord, and a crisp sound effect.
This project, while small, was a great way to put SwiftUI’s strengths to the test, focusing on:
Declarative UI: Building complex views that react to state changes.
State Management: Using @State and @Binding to drive the entire UI from a single source of truth (isOn).
Animation: Leveraging withAnimation and animation modifiers to create fluid transitions and spring physics for the pull cord.
Gestures: Implementing a DragGesture to create an interactive and intuitive pull-cord mechanism.
A Look at the Code
For those interested in how it works, the full source code is available on my GitHub. But here are a few key pieces that bring the experience to life.
The core of the app is the ContentView, which manages the isOn state. This single boolean drives everything from the background color to the sound playback.
structContentView: View { @Statevar isOn =true var body: someView { ZStack { // The background's gradient changes based on the 'isOn' state LightBeamBackground(isOn: self.$isOn) .ignoresSafeArea() // The main lightbulb button that can also toggle the state Button(action: { withAnimation { self.isOn.toggle() } }) { // ... button content } } .overlay(alignment: .topTrailing) { // The interactive pull cord is an overlay PullCord(isOn: self.$isOn) .padding(.top, -40) } .onChange(of: self.isOn) { _, newValue in // Play a sound effect whenever the state changes SoundPlayer.shared.play(newValue ? .lightOn : .lightOff) } } }
The most interactive piece is the PullCord view. It uses a DragGesture to track the user’s finger and provides physical feedback by stretching. When the drag is released, it decides whether to toggle the light based on how far it was pulled.
structPullCord: View { @Bindingvar isOn: Bool @Stateprivatevar dragOffsetY: CGFloat=0 privatelet maxDragDistance: CGFloat=140 var body: someView { ZStack(alignment: .top) { // ... visual components for the cord and handle } .gesture( DragGesture(minimumDistance: 0) .onChanged { value in let dy =max(0, value.translation.height) dragOffsetY =min(dy, maxDragDistance) } .onEnded { value in let shouldToggle = value.translation.height > maxDragDistance *0.6 withAnimation(.spring(response: 0.35, dampingFraction: 0.8)) { dragOffsetY =0 if shouldToggle { isOn.toggle() } } } ) } }
This project reaffirmed my belief in the power and elegance of SwiftUI for creating these kinds of delightful, polished user experiences.
What’s Next
This journey of exploration across the tech stack has been invaluable, but now I know where I want to build my future. I’m currently based in Calgary and am actively seeking my next role as an iOS Developer. I’m looking for a team where I can contribute my diverse experience, my passion for Apple’s ecosystem, and my drive to build beautiful, high-performing applications.
If you’re looking for a passionate iOS developer with a broad technical perspective, I would love to connect. You can find my LinkedIn profile here.
Good night, Calgary. Hopeful for new opportunities and interviews tomorrow!
Advanced Techniques with Binding: Transforming and Adapting State
While the standard property wrappers handle most state management needs, you will often encounter situations where the shape of your state doesn’t perfectly match the requirements of a SwiftUI view. For example, a view might need a Binding<String>, but your model provides a Binding<String?>. Or a view needs to bind to the associated value of an enum case.
SwiftUI’s Binding type is incredibly powerful, and with a few extension methods, we can transform bindings to fit our exact needs. Let’s use the following data model for our examples:
1 2 3 4 5 6 7 8 9 10
structItem: Hashable, Identifiable { let id =UUID() var color: Color? var status: Status enumStatus: Hashable { case inStock(quantity: Int) case outOfStock(isOnBackOrder: Bool) } }
Given a state variable like @State var item: Item, a binding to its status, $item.status, would have the type Binding<Item.Status>. Let’s see how we can manipulate this and other bindings.
Drilling Down into Bindings with Key Paths
SwiftUI has built-in support for creating bindings to the properties of a bound value. When you write $item.status, Swift is transparently applying a transform to map the Binding<Item> to a Binding<Item.Status>. This is conceptually achieved through a map function that uses a WritableKeyPath:
You rarely need to call this map function directly, as the . syntax ($item.status) is convenient shorthand for the same operation. It’s the simplest way to transform a Binding<A> into a Binding<B>.
Handling Optionals: The unwrap Extension
A very common scenario is dealing with optional state. For instance, our item.color property is a Color?, making $item.color a Binding<Color?>. However, SwiftUI’s ColorPicker view requires a Binding<Color>. It cannot work with optionals.
To bridge this gap, we can create a handy unwrap extension that transforms an optional binding Binding<A?> into a non-optional binding Binding<A>?. The result is itself an optional: it will be nil if the original state is nil, or a valid, non-optional Binding if the state has a value.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
extensionBinding { /// Transforms a `Binding<Wrapped?>` into a `Binding<Wrapped>?`. /// If the original binding's value is `nil`, this returns `nil`. funcunwrap<Wrapped>() -> Binding<Wrapped>? whereValue==Wrapped? { // If the wrapped value is nil, we can't create a binding. guardlet value =self.wrappedValue else { returnnil } // Create a new binding that gets the non-optional value // and sets the optional value on the original source. returnBinding<Wrapped>( get: { value }, set: { self.wrappedValue =$0 } ) } }
Usage:
You can use this with an if let statement to conditionally show a view that requires a non-optional binding.
var body: someView { // Only show the ColorPicker if a binding to a non-optional Color can be created. iflet colorBinding =$item.color.unwrap() { ColorPicker("Item Color", selection: colorBinding) } else { Text("This item has no color set.") } } }
A More General Solution for Enums: matching with CasePaths
The unwrap function is actually a specific version of a broader problem: how do we bind to the associated value of an enum case? An Optional is just an enum with two cases: .none and .some(Wrapped). unwrap effectively extracts the associated value from the .some case.
To create a more generic solution for any enum, we can leverage the excellent CasePaths library. A CasePath is like a “key path for an enum case,” allowing you to reliably extract associated values from and embed them back into an enum.
Building on this, we can create a matching function that returns a binding to an associated value if and only if the binding’s value is currently in that specific case.
// Requires the 'CasePaths' library: https://github.com/pointfreeco/swift-case-paths import CasePaths
extensionBinding { /// Returns a new binding focused on a specific case of an enum. /// If the original binding's value does not match the case, this returns `nil`. funcmatching<Case>( _casePath: CasePath<Value, Case> ) -> Binding<Case>? { // Attempt to extract the associated value from the current state. guardlet `case` = casePath.extract(from: self.wrappedValue) else { returnnil } // Create a new binding. returnBinding<Case>( // The getter returns the extracted associated value. get: { `case` }, // The setter embeds the new value back into the case and updates the original source. set: { `case` inself.wrappedValue = casePath.embed(`case`) } ) } }
Usage:
This powerful extension allows you to build UI that adapts to the state of an enum. For our Item.Status, we can show completely different controls for each case.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
structItemStatusView: View { @Bindingvar status: Item.Status var body: someView { VStack { // This UI will only appear and work if status is .inStock iflet quantityBinding =$status.matching(/Item.Status.inStock) { Stepper("Quantity: \(quantityBinding.wrappedValue)", value: quantityBinding) } // This UI will only appear and work if status is .outOfStock iflet onBackOrderBinding =$status.matching(/Item.Status.outOfStock) { Toggle("Is on backorder", isOn: onBackOrderBinding) } } } }
These techniques for transforming Binding are essential for writing clean, decoupled SwiftUI code. They allow your views to remain simple and focused on their specific data requirements, while your data models can remain complex and robust.
A Comprehensive Guide to State Management in SwiftUI
In SwiftUI, managing the state of your application—the data that drives your UI—is a fundamental concept. SwiftUI provides a set of powerful property wrappers that handle view updates automatically when your data changes. This guide explores the core tools: @State, @StateObject, @ObservedObject, and @Published.
Core Property Wrappers at a Glance
The following table provides a quick reference for the most common state management property wrappers in SwiftUI. Note that @StateObject, @ObservedObject, and @Published are all integral parts of the Combine framework, which SwiftUI uses for reactive programming.
Property Wrapper
Purpose
Data Type
Ownership & Lifecycle
Primary Use Case
@State
Manages simple, private state within a single view.
Value types (e.g., Struct, Enum, Int, String, Bool).
Owned and managed by the view. SwiftUI manages its storage. It may be re-initialized if the view’s identity changes in the view hierarchy. It is view-local.
Controlling the local state of UI components, such as a Toggle‘s on/off state, a TextField‘s input, or whether an alert is shown.
@StateObject
Manages a complex, private state object within a single view.
Reference types (Class) that must conform to ObservableObject.
Owned & Persisted by the View. SwiftUI ensures the object’s instance persists for the lifetime of the view’s identity, even across redraws. It can be passed to other views.
Creating and managing an instance of a complex data model (like a ViewModel) within the view that owns it.
@ObservedObject
Subscribes to an existing observable object from an external source.
Reference types (Class) that must conform to ObservableObject.
The view does not own the object; it merely “borrows” or “observes” it. Its lifecycle is managed externally.
Receiving and responding to a shared data model in a subview, where the model is managed by a parent view or another part of the app.
@Published
Automatically publishes notifications when a property’s value changes.
Any type.
Its lifecycle is tied to the ObservableObject instance it belongs to.
Marking properties within a ViewModel or shared data model that should trigger UI updates whenever they are modified.
A key distinction to remember is that a view’s @State can be destroyed and recreated if the view is removed and re-added to the view hierarchy. In contrast, @StateObject is designed to survive view redraws as long as the view maintains its identity.
Understanding the Nature of SwiftUI Views
Before diving deeper, it’s crucial to understand what a SwiftUI View is. The struct you define (e.g., struct MyView: View) is not the persistent object you see on screen. Instead, it’s a lightweight “blueprint” or “description” of your UI.
View Structs are Ephemeral: Every time SwiftUI needs to update the UI (perhaps because a @State variable changed), it re-creates your view struct and calls its body property to get a new blueprint. Creating and destroying these structs is extremely fast and low-cost.
“Redraw” = “Re-evaluating body“: When we say a “view redraws,” it’s more accurate to say that “the view’s body property is re-evaluated,” which often results in new view structs being created.
The Magic of @StateObject: Separating State from the View Struct
@StateObject was introduced to solve the problem of state being reset during view redraws. Its mechanism works as follows:
View Identity: SwiftUI uniquely identifies a view by its position and type within the View Tree. For example, “the first UserProfileView inside the VStack in ContentView.”
First-Time Creation & Storage: The very first time a view with this specific identity appears, SwiftUI sees the @StateObject property wrapper. It then:
Executes your initialization code (e.g., _viewModel = StateObject(wrappedValue: UserViewModel())) to create an instance of your ObservableObject.
SwiftUI then takes this newly created instance and stores it in a special, managed memory area associated with that specific view identity.
Subsequent Redraws: Later, when a parent view’s state changes and your view’s body is re-evaluated, a new view struct is created. However:
SwiftUI again sees the @StateObject property wrapper.
This time, it checks its internal storage and finds that an object is already associated with this view’s identity.
It skips your initialization code and simply connects the property to the pre-existing instance from its managed memory.
Practical Example: The Lifecycle of @State
The following code demonstrates how @State is tied to the view instance’s lifetime within the view hierarchy.
structCounterView: View { // A @State variable, initialized to 0 @Stateprivatevar count =0
init() { // Log when this view is created print("✅ CounterView has been initialized.") }
var body: someView { VStack { Text("Counter Value: \(count)") .font(.title) .padding() Button("Increment Count") { count +=1 } } .padding() .border(Color.blue, width: 2) // This is called when the view is removed from the view tree .onDisappear { print("❌ CounterView has disappeared.") } } }
if showCounter { // When showCounter is true, CounterView exists in the view tree CounterView() } else { // When showCounter is false, CounterView is completely removed // A placeholder Text shows the structural change Text("Counter is hidden") .foregroundColor(.gray) } Spacer() } .navigationTitle("State Lifecycle Demo") } }
How to Run and Observe
Run the ContainerView. You will see the counter, and the console will print: “✅ CounterView has been initialized.”
Click the “Increment Count” button a few times to increase the count (e.g., to 5).
Now, tap the “Show/Hide Counter”Toggle to turn it off.
The CounterView will disappear from the screen.
The console will print: “❌ CounterView has disappeared.” This confirms the view instance was destroyed.
Tap the Toggle again to turn it back on.
CounterView reappears on the screen.
The console will again print: “✅ CounterView has been initialized.” This proves that SwiftUI has created a brand new CounterView instance.
You will notice that the counter’s value has reset to 0, not the 5 you left it at.
This behavior perfectly illustrates that @State‘s storage is tied to the lifecycle of its containing view in the view hierarchy. If the view is removed, its state is lost. This is precisely the scenario where @StateObject should be used if you need the state to persist as long as the view’s identity remains the same.
I am ready to systematically review the iOS knowledge, so I have this simple Swift 6 Programming study notes.
Part 1: Core Concepts
1. Value vs. Reference Types
Types in Swift are divided into value types (struct, enum) and reference types (class). The fundamental difference lies in how their data is stored and passed.
Value Types: Each instance keeps a unique copy of its data. When you pass a value type, it is copied.
Reference Types: Instances share a single copy of their data. When you pass a reference type, a reference (or pointer) to the instance is passed.
Copy-on-Write
To optimize performance, many of Swift’s standard library value types (like Array and Dictionary) use a technique called Copy-on-Write. This means a copy is only made when the data needs to be modified; otherwise, multiple instances share the same data storage.
/* Console Output: Start ADD Making a copy of internalQueue Not making a copy of internalQueue 2 Done */
Noncopyable Types (~Copyable)
Swift 6 introduces noncopyable types to represent unique resources like file handles or network sockets, ensuring they are not accidentally duplicated.
1 2 3 4
structPerson: ~Copyable { var firstName: String var lastName: String }
Two key concepts related to noncopyable types are borrowing and consuming.
Borrowing (borrowing): Grants temporary, read-only access to a noncopyable value without transferring ownership. Borrowed values are thread-safe.
Consuming (consuming): Transfers ownership of a noncopyable value, and the original variable becomes invalid. A consuming method ends the object’s lifetime upon its return. Global instances cannot be consumed.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// The `borrowing` keyword indicates the function temporarily borrows `user` without taking ownership funcsendEmail(_user: borrowing Person) { print("Sending Email to \(user.firstName)") }
// The `consuming` keyword indicates the function consumes `user`, taking ownership funcconsumeUser(_user: consuming Person) { print("Consuming User \(user.firstName)") }
funcuserFunction() { let user =Person(firstName: "Jon", lastName: "Hoffman") sendEmail(user) // user is borrowed consumeUser(user) // user is consumed, and the variable becomes invalid afterward }
We can also create consuming methods that invalidate the instance once executed.
1 2 3 4 5 6 7 8 9
structSecrectMessage: ~Copyable { privatevar message: String init(_message: String) { self.message = message } // After this consuming method is executed, the instance is destroyed consuming funcread() { print("\(message)") } }
2. Enumerations
Enumerations define a common type for a group of related values.
Raw Values: Enum members can be prepopulated with a default value.
Pattern Matching: The switch statement makes it easy to handle different enum cases and extract their associated values.
1 2 3 4 5 6 7
let masterSwift =Product.Book(49.99, 2024, 394) switch masterSwift { case .Book(let price, let year, let pages): print("Mastering Swift was published in \(year) for \(price) and has \(pages) pages") case .Puzzle(let price, let pieces): print("A puzzle with \(pieces) pieces and sells for \(price)") }
Enum Iteration: By conforming to the CaseIterable protocol, you can iterate over all members of an enumeration.
Shorthand Syntax: Swift provides several ways to simplify closure syntax.
1 2 3 4 5 6 7 8 9 10 11
// $0, $1 represent the first and second parameters guests.map { print("hello \($0)") }
// If the closure is the only argument to a function, the parentheses can be omitted testFunction2(num: 5) { print("hello from \($0)") }
// Single-expression closures can implicitly return their result let clos7 = { (first: Int, second: Int) -> Intin first + second } print(clos7(1, 2)) // Prints 3
Escaping Closures (@escaping): When a closure is called after the function it was passed to returns, it needs to be marked with the @escaping keyword. This typically happens when the closure is stored for later use or executed in an asynchronous operation.
Throwing Errors (throws): Use the throw keyword to throw an error within a function. The function’s signature must be marked with throws.
1 2 3 4 5 6
mutatingfuncaddPlayer(player: BaseballPlayer) throws { guard player.number < maxNumber else { throwPlayerNumberError.NumberTooHigh(description: "Max number is \(maxNumber)") } // ... other checks }
Catching Errors (do-catch): Use a do-catch statement to call a function that can throw an error.
1 2 3 4 5 6 7 8
do { let player =try myTeam.getPlayerByNumber(number: 34) print("Player is \(player.firstName)") } catchPlayerNumberError.NumberDoesNotExist { print("No player has that number") } catchlet error { // Catch all other errors print("An error occurred: \(error)") }
You can also match multiple error patterns in a single catch clause:
1 2 3
catchPlayerNumberError.NumberTooHigh, PlayerNumberError.NumberTooLow { print("Number is out of range.") }
LocalizedError Protocol: Conforming to this protocol can provide richer, localized descriptions for your errors.
1 2 3 4 5 6 7 8 9 10
enumPlayerNumberError: Error, LocalizedError { // ... cases var errorDescription: String? { switchself { case .NumberAlreadyAssigned: return"Player number already assigned" // ... other descriptions } } }
Defer Statement (defer): The code within a defer block is executed just before the current scope is exited, whether by normal completion or by throwing an error. This is very useful for resource cleanup.
1 2 3 4 5 6 7 8 9
funcprocessFile() { let file = openFile() defer { closeFile(file) // Ensures the file is always closed print("File closed.") } // ... file processing code that might throw an error print("File processed.") }
5. Memory Management
Swift uses Automatic Reference Counting (ARC) to manage memory. ARC tracks the number of references to class instances. When the reference count for an instance drops to zero, the instance is deallocated, and its memory is freed.
var ref1: MyClass? =MyClass(name: "One") // Reference count is 1 var ref2: MyClass? = ref1 // Reference count is 2 ref1 =nil// Reference count is 1 ref2 =nil// Reference count is 0, instance is deallocated
Strong Reference Cycles
If two class instances hold a strong reference to each other, they will never be deallocated, causing a memory leak.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
classMyClass1_Strong { var class2: MyClass2_Strong? // ... } classMyClass2_Strong { var class1: MyClass1_Strong? // ... }
class1 =nil class2 =nil// Instances will not be released
Solutions:
Weak References (weak): Use when the referenced instance might become nil. A weak reference does not increase the reference count and must be an optional type.
Unowned References (unowned): Use when you are certain the referenced instance will never be nil during the current instance’s lifetime. An unowned reference is not optional, and accessing a deallocated unowned reference will trigger a runtime error.
Part 2: Protocol-Oriented & Functional Programming
Swift is a multi-paradigm language with strong support for Protocol-Oriented Programming (POP) and Functional Programming.
1. Object-Oriented Programming (OOP)
The three pillars of OOP are Encapsulation, Inheritance, and Polymorphism.
Inheritance: Supported by reference types (class). While powerful, complex class hierarchies can increase code complexity and coupling, making modification and maintenance difficult.
Dynamic Dispatch: When a method on a class is called, the runtime uses a virtual table (VTable) to look up and call the correct implementation. This provides flexibility but is slightly slower than a direct call.
2. Protocol-Oriented Programming (POP)
POP is a core design philosophy in Swift. It emphasizes defining blueprints using protocols rather than relying on class inheritance.
Protocol Definition: A protocol defines a blueprint of methods, properties, and other requirements.
1 2 3
protocolNameable { var firstName: String { get } }
Protocol Composition: A type can conform to multiple protocols, combining different functionalities.
Protocol Inheritance: Protocols can also inherit from other protocols, aggregating multiple requirements.
Compared to class inheritance, POP offers greater flexibility and modularity, helping to avoid bloated base classes.
3. Protocols and Protocol Extensions
Type Checking and Casting: You can use is and as? to check if an instance conforms to a protocol.
1 2 3
for person in people where person isSwiftProgrammer { print("\(person.firstName) is a Swift Programmer") }
Protocol Extensions: You can extend protocols to provide default implementations for methods. Types conforming to the protocol automatically gain this functionality.
1 2 3 4 5 6 7 8
protocolDog { var name: String { get } } extensionDog { funcspeak() -> String { return"Woof Woof" } }
Any vs any:
Any: Can represent a value of any type, including function and optional types.
any: Used to modify a protocol, representing an existential type. It allows you to store values of different types that conform to the same protocol in a container and supports dynamic dispatch.
Implicitly Opened Existentials: Swift 6 allows the compiler to implicitly open existential types, simplifying operations on protocol arrays.
1 2 3 4 5 6 7
protocolDrawable { funcdraw() } // No manual casting needed, protocol methods can be called directly funcdrawAll(_items: [any Drawable]) { for item in items { item.draw() } }
4. Generics
Generic code enables you to write flexible, reusable functions and types that can work with any type.
Generic Functions:
1 2 3
funcswapGeneric<T>(a: inoutT, b: inoutT) { let tmp = a; a = b; b = tmp }
Associated Types (associatedtype): Used in a protocol as a placeholder for a type that is specified only when the protocol is adopted.
Conditional Extensions and Conformance: You can add extensions to a generic type that are only available if the generic parameter meets certain conditions.
1 2 3 4 5 6 7 8 9 10 11
// Add the sum method only if T is a numeric type extensionListwhereT: Numeric { funcsum() -> T { items.reduce(0, +) } }
// List conforms to Equatable only if T also conforms to Equatable extensionList: EquatablewhereT: Equatable { staticfunc== (l1: List, l2: List) -> Bool { // ... comparison logic } }
5. Functional Programming
Core principles of functional programming include:
Immutability: Prefer constants (let) to avoid direct state modification.
1 2
let numbers = [1, 2, 3, 4, 5] let doubled = numbers.map { $0*2 } // The `numbers` array itself is not changed
Pure Functions: Always produce the same output for the same input and have no side effects.
1 2 3
funcadd(_first: Int, _second: Int) -> Int { return first + second }
Higher-Order Functions: Functions that take other functions as arguments or return them, such as map, filter, and reduce.
Function Composition: Combining multiple functions into a new one.
1 2 3 4 5
infixoperator>>> func>>> <A, B, C>(lhs: @escaping (A) -> B, rhs: @escaping (B) -> C) -> (A) -> C { return { rhs(lhs($0)) } } let addOneToString = addOne >>> toString
Currying: Transforming a function that takes multiple arguments into a sequence of functions that each take a single argument.
1 2 3 4 5
funccurriedAdd(_a: Int) -> (Int) -> Int { return { a +$0 } } let addTwo = curriedAdd(2) let result = addTwo(3) // result is 5
Recursion: A function calling itself to solve a problem.
1 2 3 4
funcfactorial(_n: Int) -> Int { if n <=1 { return1 } return n * factorial(n -1) }
Part 3: Modern Concurrency
Swift provides a concurrency model ranging from the low-level GCD to the modern async/await structured approach.
1. Grand Central Dispatch (GCD)
GCD is a low-level C API that manages tasks via queues.
Concurrency: Multiple tasks starting, running, and completing in the same time period.
Parallelism: Multiple tasks running at the exact same moment, which requires a multi-core processor.
Queue Types:
Serial Queues: Tasks are executed one at a time in FIFO order. Often used to synchronize access to a shared resource.
Concurrent Queues: Tasks start in order but can run concurrently. The system determines the number of concurrent tasks.
Main Dispatch Queue: A globally available serial queue that executes tasks on the application’s main thread, typically used for UI updates.
Using Queues:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// Create a concurrent queue let cqueue =DispatchQueue(label: "cqueue.example", attributes: .concurrent) // Create a serial queue let squeue =DispatchQueue(label: "squeue.example")
// Execute a task asynchronously without blocking the current thread cqueue.async { performCalculation(tag: "async1") }
// Switch from a background thread to the main thread to update the UI squeue.async { let resizedImage = image.resize() DispatchQueue.main.async { picView.image = resizedImage } }
Advanced Tools:
DispatchGroup: Coordinate the completion of multiple asynchronous tasks.
group.notify(queue: .main) { print("All tasks are complete") }
Barrier: Create a synchronization point in a concurrent queue. All tasks submitted before the barrier complete before the barrier task executes. Tasks submitted after the barrier wait for it to finish.
1 2 3
queue.async(flags: .barrier) { // This task waits for previous tasks and blocks subsequent ones }
DispatchSemaphore: Control the number of concurrent accesses to a shared resource.
1 2 3 4 5 6
let semaphore =DispatchSemaphore(value: 1) // Allow only one thread to access funcaccessSharedResource() { semaphore.wait() // Request access, wait if count is 0 // ... access shared resource ... semaphore.signal() // Finished, release the resource }
2. Structured Concurrency (async/await)
Swift 6 emphasizes using structured concurrency to write safer, more readable asynchronous code, preventing issues like data races.
async and await
async: Marks a function as asynchronous, meaning it can be suspended during its execution.
await: Used to call an async function, indicating that the current task might pause here to wait for the result of the asynchronous function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
funcretrieveUserData() async -> String { print("Retrieving user data") try?awaitTask.sleep(nanoseconds: 2_000_000_000) return"User Data Retrieved" }
// Serial execution let data =await retrieveUserData()
A Task represents a unit of work that can be run asynchronously.
Creating a Task:
1 2 3 4
Task { let data =await retrieveUserData() print("Data: \(data)") }
Detached Task (Task.detached): Creates a top-level task that does not inherit the context (like actor isolation) of its creation point.
Task Cancellation: Tasks can be cancelled externally. In a long-running loop, you should periodically check Task.isCancelled and exit gracefully.
1 2 3 4 5 6 7 8 9 10
functestCancelTask() asyncthrows { for i in0..<10 { ifTask.isCancelled { print("Task was cancelled, cleaning up") throwCancellationError() } print("Loop \(i)") await retrieveUserData() } }
Task Groups
Used for creating a dynamic group of concurrent tasks and waiting for all of them to complete.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
functaskGroup() async -> [String] { returnawait withTaskGroup(of: String.self) { group in let users = ["Jon", "Heidi", "Kailey", "Kai"] for user in users { group.addTask { returnawait retrieveUserData(user) } } var data = [String]() forawait result in group { data.append(result) } return data } }
Actors
Actors are a special kind of reference type that protect their mutable state from concurrent access, preventing data races. Access to an actor’s internal state is asynchronous and serialized.
let account =BankAccount(5000) await account.deposit(amount: 100) let newBalance =await account.getBalance() print("New Balance: \(newBalance)")
Sendable Types
The Sendable protocol marks types whose values can be safely passed between concurrency domains (e.g., from one actor to another).
Automatic Conformance: Swift’s core value types (Int, String, etc.), structs and enums containing only Sendable values, and actor types automatically conform to Sendable.
Manual Conformance: A class can conform to Sendable if it is final, all its properties are immutable constants (let), and the types of those properties also conform to Sendable.
Part 4: Advanced & Specialized Features
1. Property Observers and Wrappers
Property Observers (willSet/didSet): Execute code before (willSet) or after (didSet) a property’s value is set.
1 2 3 4 5 6 7 8 9 10 11 12
structMyStruct { var myProperty: String { willSet(newName) { print("Preparing to change from \(myProperty) to \(newName)") } didSet { if oldValue != myProperty { print("Value changed from \(oldValue) to \(myProperty)") } } } }
Property Wrappers (@propertyWrapper): Encapsulate the storage and logic of a property into a separate type to reduce code duplication.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
@propertyWrapper structMyPropertyWrapper<T> { privatevar value: T var wrappedValue: T { get { /* return the value */ } set { /* modify the value */ } } init(wrappedValueinitialValue: T) { self.value = initialValue } }
structMyExample { @MyPropertyWrappervar number: Int }
2. Key Paths and Dynamic Member Lookup
Key Paths: Provide a type-safe way to reference a property of a type. The syntax is \TypeName.propertyName.
1 2 3 4 5 6 7 8 9 10 11
structBasketballTeam { var city: String } let cityKeyPath = \BasketballTeam.city var team =BasketballTeam(city: "Boston") let teamCity = team[keyPath: cityKeyPath] // "Boston"
// Simpler syntax in map/filter let names = people.map(\.name) let adults = people.filter { $0.age >17 } // Traditional way let adultsWithKeyPath = people.filter { $0[keyPath: \.age] >17 }
Dynamic Member Lookup (@dynamicMemberLookup): Allows a type to access members dynamically using dot syntax, even if those members are not explicitly defined at compile time.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
@dynamicMemberLookup structBaseballTeam { let city: String let nickName: String subscript(dynamicMemberkey: String) -> String { switch key { case"fullname": return"\(city)\(nickName)" default: return"Unknown" } } } let team =BaseballTeam(city: "Boston", nickName: "Red Sox") print(team.fullname) // Prints "Boston Red Sox"
3. Custom Subscripting
Allows you to access instances of a type by index, similar to an array or dictionary.
Result builders are a special syntax transformation that lets you build a complex result from a sequence of statements, commonly used for creating Domain-Specific Languages (DSLs), like SwiftUI’s view builder.
let result = buildString { "Hello," "Mastering" "Swift!" } print(result) // "Hello, Mastering Swift!"
5. Reflection
Swift’s Mirror API allows you to inspect the properties, types, and values of an instance at runtime. Swift’s reflection is read-only, in keeping with its principle of type safety.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
let person =Person(firstName: "Jon", lastName: "Hoffman", age: 55) let mirror =Mirror(reflecting: person)
for (label, value) in mirror.children { print("Property: \(label ??"Unknown"), Value: \(value)") }
// Can be used to implement a generic serialization function funcserialize<T>(_value: T) -> [String: Any] { let mirror =Mirror(reflecting: value) var result = [String: Any]() for child in mirror.children { iflet propertyName = child.label { result[propertyName] = child.value } } return result }
6. Regular Expressions
Swift provides modern and powerful support for regular expressions.
Literal Syntax:
1 2 3 4 5 6
let pattern =/\b\w+\b/ let text ="Hello from regex literal" let matches = text.matches(of: pattern) for match in matches { print("-- \(text[match.range])") }
RegexBuilder: Construct complex regular expressions in a declarative way.
1 2 3 4 5 6 7 8
import RegexBuilder
let pattern =Regex { Anchor.wordBoundary OneOrMore(.word) "@" // ... more components }
Capturing: You can define references to capture matched parts and perform type conversions.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
let animalTypeRef =Reference(Substring.self) let ageRef =Reference(Int.self)
let pattern =Regex { "I am a " Capture(as: animalTypeRef) { OneOrMore(.word) } " who is " TryCapture(as: ageRef) { OneOrMore(.digit) } transform: { Int($0) } // ... }
Vim is a powerful and versatile text editor that offers a wide range of features and customization options. In this article, we will explore some useful tips and techniques from the book “Modern Vim” to help you become more efficient and productive in your Vim editing.
Introduction
Vim has a unique set of keyboard shortcuts and commands that can enhance your editing experience. Here are some key concepts and shortcuts to keep in mind:
<C-p> represents pressing the Control key followed by the letter “p.”
Operators are commands used to perform actions on text. Some common operators include:
1 2 3 4 5 6 7 8 9 10 11 12 13
`c` for change `d` for delete `y` for yank (copy) `~` for swapping case `gu` for making text lowercase `gU` for making text uppercase `!` for filtering text through an external program `=` for text formatting `gq` for text formatting with line wrapping `>` for shifting text right `<` for shifting text left `zf` for defining a fold `g@` for calling a function set with the 'operatorfunc' option
The Meta key:
On macOS, it refers to the Option key.
On Windows, it refers to the Alt key.
Getting Modern Vim
To get the most out of Vim, it’s recommended to use Neovim and the neovim-remote tool developed by Marco Hinz. This tool allows remote control of Neovim processes.
Installing Plugins
Plugins are a great way to extend Vim’s functionality. Here are some insights on managing plugins:
Understanding Scripts, Plugins, and Packages:
You can manually load a script using the :source {path} command, where {path} is the location of the script.
Vim automatically sources scripts located in specific locations on disk when it starts up.
Your vimrc file is one of the first scripts to be loaded, making it an ideal place to configure your startup settings.
Prior to recent versions of Vim, managing the runtimepath to include plugins was not convenient. However, you can now use the :set runtimepath+=$VIMCONFIG/arbitrary/demo-plugin command to add a plugin to the runtimepath.
Pressing <C-]> will jump to the specified anchor in Vim’s documentation, and you can use <C-o> to quickly jump back to the previous location. These commands allow you to navigate Vim’s documentation similar to interacting with a web page.
After installing a new plugin, you only need to run :helptags once. Vim will then use the generated tags file to find the documentation for that plugin.
Installing Plugins to Your Package:
Note that if you install a new plugin into the start directory while Vim is running, you won’t be able to use it immediately. Restarting Vim will add the new plugin to the runtimepath and make it available.
The unimpaired plugin comes with documentation, but Vim doesn’t know where to find the appropriate files. You can fix this issue by running the :helptags ALL command (:help :helptags).
You can suppress error messages by running :silent helptags ALL.
By default, optional plugins are not loaded. Use the :packadd command to activate a plugin (e.g., :packadd vim-scriptease).
Managing Plugins with minpac:
Typing :call minpac#update() can be cumbersome. You can create custom commands to make it more convenient:
command! PackUpdate call minpac#update()
command! PackClean call minpac#clean()
Opening Files
Efficiently opening and navigating files is crucial for an effective editing workflow. Let’s explore two techniques:
Finding Files Using Fuzzy Path Matching:
You can use <C-x>, <C-v>, or <C-t> to open a file in a horizontal split, vertical split, or new tab, respectively.
The rg --files command (Ripgrep) filters out files ignored by Git, Mercurial, and Subversion repositories.
Finding Files Semantically:
Open files in separate windows using the -O flag. For example:
vim -O file1.txt file2.txt
You can define file-to-type mappings in a .projections.json file. For instance:
"app/models/*.js": { "type": "model" }
Vim provides navigation commands specific to file types, such as:
``` :Etype - Opens the specified type in the current window :Stype - Opens the specified type in a horizontal split :Vtype - Opens the specified type in a vertical split :Ttype - Opens the specified type in a new tabpage
-These navigation commands are my preferred way to navigatecodebases,and you can add navigationcommandsas needed for different filetypes. -JumpingtoanAlternateFile: -The Projectionist plugin allows you to create links between related files. Once you specify the relationship between a file and itsalternatefile,you can follow the linkbyrunningthe`:A`command.
## Working with the QuickfixList
The quickfix list is a powerful feature in Vim that allows you to efficiently handle build errors,lintingresults,and file search results. Let'sexploresometechniques:
-Running a Build and NavigatingFailures: -TheDispatchplugin,introducedin2013,provides asynchronous command execution in Vim when it didn't natively support it. Make sure to check out the plugin's release andthe"dispatch.vim"screencast. -LintingtheCurrentFile: -Learn about the Asynchronous LintingEngine(ALE),a powerful plugin for lintingcodeinVim. -You can use the `]w`and`[w` mappings to quickly navigate between warnings. Error messages are displayed at thebottomofthescreenas you access each warning. -Neomake is another linting plugin that runs asynchronously. It supports running commands acrosstheentireproject,not just on individual files. -SearchingFileswithGrep-Alikes: -The`:Grepper` command provides a powerful way to search for patterns in files. Forexample,running`:Grepper-cword` with the word "Waldo" under the cursor will prompt you for searchoptions.
##Neovim'sBuilt-InTerminalEmulator
Neovim comes with a built-in terminal emulator that allows you to interact with programs running in the terminal. Let's dive intosometerminal-relatedtechniques:
-GrokkingTerminalMode: -Neovim introduces a new modecalledTerminalmode,where you can interact with programs running in thebuilt-interminalemulator. -Usethe`:terminal` command to open aterminalbuffer. -When you create a terminalbuffer,you start in normal mode.Pressing`i` switches to terminal mode,indicatedbythe
`-- TERMINAL --` prompt in the bottom left corner. Press `<C-><C-n>` to switch back tonormalmode. -Running Programs in a TerminalBuffer: -Usethe`:read!{cmd}` command to capture theoutputof a command in anexistingbuffer. -The`:terminal{cmd}` command is a new feature in Neovim. It runs the specified command in a new terminal buffer. You can abbreviateitas`:te{cmd}`. -To switch between a terminal buffer and a regularbuffer,use`<C-^>`(`:hctrl-^`). -Try stopping a process in the terminal buffer usingthe`:bwipeout!`command(e.g.,`:5bwipeout` to stop the topprocess). -WhenyouexitNeovim,any running processes in terminal buffers are also closed. -Note that if you suspendNeovim(`<C-z>`),all processes running in terminalbufferswillbesuspendedas well. They will resume when you resume Neovim.
-Managing Windows That Contain TerminalBuffers: -Opening a terminal buffer withthe`:terminal` command takes over the current window and hides the buffer that was previously displayed. This behavior is similar to the`:edit{file}`command. -Ifyouuse`:te`,it creates a buffer. However,`:te` does not create abuffer. -Usethe`tnoremap` command to create mappings that work only in terminal mode. With thesemappings,you can switch to anotherwindowbypressing`<M-h>`,`<M-j>`,`<M-k>`,or`<M-l>`regardlessof whether you are innormalmodeorterminalmode.
-Using Normal Mode Commands inaTerminalBuffer: -You can use the `yi`` command to copy the text within backticks to Vim's unnamed register and then paste it using`p` in the terminal atthecursorposition. -You can prepend a namedregister(e.g.,`"a`)or use special registers like`"*` to reference the system clipboard when using yankandputcommands. -The terminal buffer is nowhidden,but you can quickly switch back to it using`<C-^>`(`:hctrl-^`).
-Sending Commands to a TerminalBuffer: -Activate the window containing the terminal buffer running the web server and runthefollowingcommand: -`:echob:terminal_job_id` -This tells us that thejobIDis1,whichwecanuseas the first argument whencalling`jobsend({job},{data})`. -To restart the web server,runthecommand: -`:calljobsend(1,"\<C-c>npmrunserver\<CR>")`
##Sessions
Sessions in Vim allow you to save and restore your editing environment. Let'sexploresession-relatedtechniques:
-SavingandRestoringSessions: -Afteropeningthe`app.js`and`app-test.js` files in adjacent windows,usethe`:mksession!`command(`:h:mksession`) to save the session. -Restart Vim with the `-S` flag to load thesession: -`vim-S` -If you like the ideaofautomaticallyrecordingsessions,consider installing Tim Pope's Obsession plugin. You can install it in your bundledirectory: -`cd$VIMCONFIG/pack/bundle/start` -`gitclonehttps://github.com/tpope/vim-obsession.git`
-Making Undo Persist Between Sessions: -Bydefault,undo history is not preservedbetweensessions.However,you can use autocommands to disable the undofile for files matching specific patterns.Forexample,thefollowingisan example script that disables persistent undo in all files in the `/tmp`directory: -`--forget-undo-in-tmpfile.vim` -``` augroupvimrc autocmd! autocmdBufWritePre/tmp/*setlocalnoundofile augroupEND
Restarting Terminal Processes When Resuming a Session:
You can rename a terminal buffer using the :file {name} command (:help :file_f). Activate the window containing the terminal buffer running the web server and run:
:file term://PORT=3001 npm run server
Configuring Vim
Customizing Vim’s behavior can greatly enhance your editing experience. Here’s a technique to respond to events using autocommands:
Using Autocommands to Respond to Events:
Vim triggers the BufReadPost command (:h BufReadPost) after reading a file into a buffer. If the file path matches the pattern defined in our autocommand, Vim executes the specified {command}. Since we use a wildcard * in this example, the autocommand applies to all buffers.
Sometimes, there might be more suitable events. For such cases, you can achieve similar results by listening to the FileType event (:h FileType).
autocmd BufWritePre /tmp/* setlocal noundofile
The autocommand we defined is triggered by the User event with the pattern ProjectionistActivate. The User event doesn’t trigger automatically, but you can trigger such events yourself:
:doautocmd User ProjectionistActivate
In this article, we’ve explored various tips and techniques from “Modern Vim” to help you enhance your Vim editing skills. By incorporating these techniques into your workflow, you can become a more efficient and productive Vim user.