Folder When to use
feature New user-facing features and capabilities
fix Bug fixes, technical corrections, error handling
improvements
| import Foundation | |
| import Combine | |
| let fileNames = ["quote1", "quote2", "quote3"] | |
| class QuoteFetcher: ObservableObject { | |
| struct Quote: Codable { let quoteText: String } | |
| @Published var quotes: [Quote] = [] | |
| var subscriptions = Set<AnyCancellable>() | |
| /// Universal escape hatch. | |
| /// | |
| /// [Original source](https://stackoverflow.com/a/78894560/4370543) | |
| struct UncheckedSendable<T>: @unchecked Sendable { | |
| let unwrap: T | |
| init(_ value: T) { unwrap = value} | |
| } | |
| // A sample of usage with `SendBirdCalls` iOS SDK. Where it was impossible to have async version (was a crash). |
| @usableFromInline static func generateMockedSid() -> MockedIdentity { | |
| let sidString = UUID().uuidString | |
| let json = #"{"stringValue":"\#(sidString)"}"# | |
| let jsonData = Data(json.utf8) | |
| // swiftlint:disable:next force_try | |
| return try! JSONDecoder().decode(MockedIdentity.self, from: jsonData) | |
| } |
| private extension Collection { | |
| subscript(safe index: Index) -> Element? { | |
| indices.contains(index) ? self[index] : nil | |
| } | |
| } |
| // ✅ Should be written on the same line (short, lightweight, commonly used) | |
| /// These annotations are short and typically used with a single variable or function. | |
| /// They don’t add much complexity and keep the code concise. | |
| @State private var isLoading = false | |
| @Binding var isPresented: Bool | |
| @ObservedObject var viewModel: MyViewModel | |
| @Environment(\.dismiss) var dismiss | |
| @ViewBuilder var body: some View |
| import SwiftUI | |
| struct CommentEditor: View { | |
| @Binding var commentText: AttributedString | |
| var body: some View { | |
| TextEditor(text: $commentText) | |
| } | |
| } |
| import SwiftUI | |
| /// [Original source](https://www.youtube.com/shorts/EDd6Hr_F99o) | |
| struct ContentView: View { | |
| @State private var count = 1 | |
| var body: some View { | |
| VStack(spacing: 10) { | |
| Text("^[\(count) Person](inflect: true)") | |
| .font(.title) |
| // Demonstrates filtering and unwrapping optionals in a sequence with `for case let`. | |
| // Equivalent to `compactMap`, but shows pattern matching directly in the loop. | |
| let strings: [String?] = ["pizza", nil, "cola"] | |
| for case let string? in strings { | |
| print(string) // pizza, cola | |
| } |
| // Demonstrates how to use loop labels to exit from nested loops. | |
| // Useful when you need to break out of multiple levels of iteration at once. | |
| let groups: [[String]] = [ | |
| ["apple", "banana", "pear"], | |
| ["grape", "the one", "melon"], | |
| ["cherry", "plum", "mango"] | |
| ] | |
| outer: for group in groups { |