Snippets·Free
Swift snippets
20 Swift snippets: optionals, enums and Result, protocols with extensions, async/await and URLSession, Codable, SwiftUI views and state, an @Observable model and XCTest. Some need Swift 5.9 or iOS 17.
Free to install.
If PocketCode is installed on your Android device, this opens the page in the app.
Preview
5 of 20
Unwrap with if letiflet
Run a block only when the optional has a value
if let ${1:user} = ${2:currentUser} {
$0
}Switch over an enumswitchenum
Switch with one case per enum value
switch ${1:direction} {
case .${2:north}:
$0
case .${3:south}:
break
}Protocol with a default implementationprotoc
Protocol plus an extension that every conforming type gets for free
protocol ${1:Describable} {
var ${2:summary}: String { get }
}
extension ${1:Describable} {
func describe() {
print(${2:summary})
}
}Async throwing functionasyncf
Function called with try await
func ${1:fetchUser}(id: ${2:Int}) async throws -> ${3:User} {
$0
}Task with error handlingtaskb
Start async work from synchronous code and catch its errors
Task {
do {
${1:try await load()}
} catch {
print(error)
}
}Changes in 1.0.0
First version.