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 / MapToUnwrap.swift
Last active October 24, 2025 07:36
Map to unwrap an optional value #tip #swift-api
// Demonstrates how `map` can be used to unwrap an optional value and execute code if it exists.
var string: String? = "maybe"
// This...
if let string {
print(string)
}
// Becomes...
@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!
@dterekhov
dterekhov / PlaygroundDirective.swift
Last active October 24, 2025 07:35
Using Playground macro in Swift project #tip #swiftui
// Demonstrates how to use the `#Playground` directive right inside your main project.
// The code inside this block executes immediately when run in Playgrounds.
import SwiftUI
import Playgrounds
#Playground {
print("Hello")
}
@dterekhov
dterekhov / APITestingPlayground.swift
Last active October 24, 2025 07:34
Using Playground macro #tip #swiftui
// Demonstrates how to use `#Playground` blocks to test async API calls directly in Playgrounds.
import Foundation
import Playgrounds
#Playground("Categories") {
let apiService = APIService(urlString: "https://api.escuelajs.co/api/v1/categories")
Task {
let categories: [Category] = try await apiService.getJSON(dateDecodingStrategy: .iso8601)
@dterekhov
dterekhov / GlassEffectsDemo.swift
Created October 17, 2025 12:54
Glass effects #swiftui
// Demonstrates how to use `.glassEffect()` in SwiftUI to create a translucent background effect.
// Available in iOS 18+.
import SwiftUI
struct GlassEffectsView: View {
var body: some View {
NavigationStack {
VStack {
Text("Hello World")
@dterekhov
dterekhov / CompositingGroupBlendMode.swift
Created October 17, 2025 19:08
Punch-out effect with compositingGroup and .blendMode(.destinationOut) #swiftui
// Creates a "punched-out" look by grouping layers with `compositingGroup()`
// and cutting out content using `.blendMode(.destinationOut)`. The shadow is
// applied to the merged result of the group.
import SwiftUI
struct CompositingGroupDemoView: View {
var body: some View {
ZStack {
LinearGradient(colors: [.gray.opacity(0.2), .gray.opacity(0.5)],
@dterekhov
dterekhov / RenderMarkdownInXcode.md
Created October 17, 2025 19:12
Render Markdown files directly in Xcode #xcode

Tip: Enable Markdown rendering for .md files in Xcode

By default, Xcode does not render Markdown files as formatted text. To enable Markdown rendering for your project:

  1. In the project root, create a file named: .xcodesamplecode.plist

  2. Add the following XML content:

@dterekhov
dterekhov / HierarchicalStyles.swift
Last active October 24, 2025 07:33
Hierarchical color styles #tip #swiftui
// Demonstrates hierarchical color variants (primary → quinary).
// Useful for subtle gradients of the same base color without defining new values.
import SwiftUI
struct HierarchicalStylesView: View {
var body: some View {
VStack(spacing: 16) {
Circle().foregroundStyle(Color.red)
Circle().foregroundStyle(Color.red.secondary)
@dterekhov
dterekhov / PreviewMacroExample.swift
Last active October 23, 2025 20:10
Modern SwiftUI Previews with Preview macro #swiftui
// ✅ Tip:
// You can right-click an old PreviewProvider block and choose “Convert to Preview Macro” in Xcode 16+.
// This enables advanced preview configurations with less boilerplate and full live state support.
// Demonstrates the modern replacement for PreviewProvider using #Preview macro.
// You can configure traits, states, and multiple previews with concise syntax.
import SwiftUI
struct CounterView: View {
@dterekhov
dterekhov / AccessibilityThatFits.swift
Last active October 24, 2025 07:33
Adaptive text width with ViewThatFits #accessibility #swiftui
// ✅ Tip:
// ViewThatFits automatically selects the first view that fits within the container, making it perfect for adaptive accessibility layouts.
// Automatically adjusts text to fit the available horizontal space.
// Ideal for dynamic content that varies in length (e.g., dates, titles).
import SwiftUI
struct AccessibilityThatFitsView: View {
private var titles: [String] {