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 / EnumCodableExample.swift
Last active October 24, 2025 06:48
Enum Codable synthesis #persistence #swift-api
import Foundation
enum Command: Codable {
case load(key: String)
case store(key: String, value: Int)
}
@main
struct Demo {
static func main() throws {
@dterekhov
dterekhov / AsyncSequenceMethodsDemo.swift
Created October 23, 2025 09:27
Compact async sequence demo showing outputs for all main transformation methods (map, filter, reduce, etc.) #swift-concurrency
import Foundation
// Helper async sequence emitting numbers 1...5
let numbers = [1, 2, 3, 4, 5].async
@main
struct Demo {
static func main() async {
print("map:", await numbers.map { $0 * 2 }.reduce([], +)) // [2, 4, 6, 8, 10]
print("filter:", await numbers.filter { $0.isMultiple(of: 2) }.reduce([], +)) // [2, 4]
@dterekhov
dterekhov / SafeAreaInsetExample.swift
Created October 23, 2025 09:03
Be able to scroll content under the bottom bar with safeAreaInset modifier #swiftui #tip
import SwiftUI
struct GradientBuilderView: View {
@State private var gradientName = "New Gradient"
@State private var stopCount = 7
@State private var isEditing = false
var body: some View {
ScrollView {
VStack(spacing: 20) {
@dterekhov
dterekhov / CreateCodeSnippet.md
Last active October 24, 2025 06:53
"Create code snippet…" comand in #xcode #tip

Please check the attached images.

@dterekhov
dterekhov / LLDBPropertyWrapperTip.md
Last active November 7, 2025 21:24
Access property wrapper values in LLDB console #debug #xcode

Console Debugging Tip for Swift Developers

Sometimes the debugger cannot display a value inside a property wrapper (e.g., @State, @Published, etc.).
You can directly access the underlying storage variable using the underscore (_) prefix in LLDB.

Example

(lldb) po self._viewModel.startPointAngle/*
 Console debugging tip for Swift developers
@dterekhov
dterekhov / TypedThrows.swift
Created October 20, 2025 12:36
Typed throws #swift-api
// MARK: - Swift 6: Typed throws
// Define a specific error type for integer parsing.
enum IntegerParseError: Error {
case nonDigitCharacter(String, index: String.Index)
}
/// Parses a string into an integer, throwing a specific typed error if parsing fails.
///
/// - Parameter string: The string to parse.
@dterekhov
dterekhov / ContainerRelativeFrame.swift
Created October 20, 2025 12:31
New containerRelativeFrame - replacement older frame(maxWidth: .infinity) #swiftui
import SwiftUI
// MARK: - 1. Basic fixed frame aligned to container
struct BasicsView: View {
var body: some View {
Color.red
.frame(width: 200, height: 300)
.containerRelativeFrame([.horizontal, .vertical], alignment: .topLeading)
}
}
@dterekhov
dterekhov / MeshGradient.swift
Last active October 24, 2025 07:17
New visual effects in iOS 18 #swiftui
MeshGradient(
width: 3,
height: 3,
points: [
[0.0, 0.0], [0.5, 0.0], [1.0, 0.0],
[0.0, 0.5], [0.9, 0.3], [1.0, 0.5],
[0.0, 1.0], [0.5, 1.0], [1.0, 1.0]
],
colors: [
.black, .black, .black,
@dterekhov
dterekhov / OptionalEnvironmentExample.swift
Created October 20, 2025 08:01
Safely access environment objects in SwiftUI #swiftui
import SwiftUI
// MARK: - Safe environment access example
struct LibraryView: View {
// Retrieves a non-optional environment value; throws if not set
@Environment(Library.self) private var library
var body: some View {
// Your view content here
Text("Library loaded: \(library.name)")
@dterekhov
dterekhov / StructuralTypes.swift
Last active October 24, 2025 07:36
Don't create small structures, use Structural types #tip #swift-api
// Demonstrates that tuple type aliases with identical structures are considered equal by Swift.
typealias Person = (name: String, age: Int)
typealias Address = (street: String, houseNumber: Int)
let abbey = Person(name: "Abbey Road", age: 29)
let home = Address(street: "Abbey Road", houseNumber: 29)
if abbey == home {
print("They are structurally equal!") // Works!