Skip to content

Instantly share code, notes, and snippets.

View dterekhov's full-sized avatar

Dmitry Terekhov dterekhov

  • Russia, Voronezh
View GitHub Profile
@dterekhov
dterekhov / PublisherFromCollection.swift
Last active October 17, 2025 11:54
Swift, Combine: Create publisher from collection #combine
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>()
@dterekhov
dterekhov / UncheckedSendableCompletion.swift
Last active October 23, 2025 19:13
Swift, Concurrency: Universal escape hatch from Swift structured concurrency #swift-concurrency
/// 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).
@dterekhov
dterekhov / RawJSONToModel.swift
Last active October 24, 2025 07:40
Raw JSON string to specific model #tip #swift-api
@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)
}
@dterekhov
dterekhov / SafeCollectionSubscript.swift
Last active October 24, 2025 07:40
Safe collection subscript accessor #utility #swift-api
private extension Collection {
subscript(safe index: Index) -> Element? {
indices.contains(index) ? self[index] : nil
}
}
@dterekhov
dterekhov / GitRepoFolderConventions.md
Last active October 24, 2025 07:49
Git Repo Folder Conventions #git

Commit Folder Naming Guide


Folder When to use


feature New user-facing features and capabilities

fix Bug fixes, technical corrections, error handling improvements

@dterekhov
dterekhov / SameLineWrittenSwiftStyleguide.swift
Last active October 24, 2025 07:39
Same line written styleguide #best-practices #swift-api
// ✅ 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
@dterekhov
dterekhov / RichTextEditor.swift
Last active October 24, 2025 07:39
Rich text editor #text #swiftui
import SwiftUI
struct CommentEditor: View {
@Binding var commentText: AttributedString
var body: some View {
TextEditor(text: $commentText)
}
}
@dterekhov
dterekhov / AutomaticGrammarAgreement.swift
Last active October 24, 2025 07:38
Automatic grammar agreement #tip #swift-api
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)
@dterekhov
dterekhov / ForCaseLetOptionalFiltering.swift
Last active October 24, 2025 07:38
Filtering and unwrapping optionals in a sequence with `for case let` #tip #swift-api
// 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
}
@dterekhov
dterekhov / LoopLabels.swift
Last active October 24, 2025 07:37
Loop labels to break from nested loops #tip #swift-api
// 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 {