Property Wrappers in Swift

Property wrappers add a layer of separation between code that manages how a property is stored and the code that defines a property. A property wrapper is a type that wraps a property, adding additional behavior or validation. They were introduced in Swift 5.1. Key Components @propertyWrapper Attribute: Marks a type as a property wrapper wrappedValue: The underlying value being wrapped projectedValue (optional): Additional functionality exposed via $ prefix Common Use Cases Property wrappers are commonly used for:...

February 4, 2025

SwiftUI - Button Styles, View Modifiers, Animatable

This playground demonstrates key SwiftUI and Combine concepts including: Network requests using async/await and Combine MVVM architecture SwiftUI views and navigation Protocol-based networking JSON decoding struct SwiftUIView: View { @State private var range: ClosedRange = 0…5 @State var shakeNUmber: Int = 10 var body: some View { NavigationView { VStack { Button { withAnimation(.easeInOut(duration: 0.5)) { // Use a known animation let random = Int.random(in: 10…20) range = 0…random } } label: { Text(“Randomize”) } ....

February 4, 2025

SwiftUI - Preferences, PreferenceKey, AlignmentGuide, coordinateSpace, matchedGeometryEffect

SwiftUI provides several powerful mechanisms for layout control and view coordination. Let’s explore some key concepts: matchedGeometryEffect matchedGeometryEffect allows you to create smooth animations between views that share the same identity. This is particularly useful for transitions where you want elements to morph from one state to another. In the example below, we create a highlight effect that follows selected buttons: struct MatchedGeometryView: View { @Namespace var namespace let groupID = "highlight" var body: some View { VStack { Button("Sign Up") {} ....

February 4, 2025

SwiftUI and Combine Example

This playground demonstrates key SwiftUI and Combine concepts including: Network requests using async/await and Combine MVVM architecture SwiftUI views and navigation Protocol-based networking JSON decoding import Foundation import SwiftUI import PlaygroundSupport import Combine // MARK: - Networking // Protocol defining network operations protocol NetworkManaging { func fetch<T:Decodable>(url: String) throws -> T func uploadFile(url: String) } extension NetworkManaging { func uploadfile(url: String) { } } // MARK: - Models // Post model conforming to Decodable for JSON parsing and Identifiable for SwiftUI lists struct Post: Decodable, Identifiable { let id: Int let title: String } extension Post: Hashable { func hash(into hasher: inout Hasher) { hasher....

February 4, 2025

Learning/DesignPatterns

Design Patterns implemented in Swift 5.0 A short cheat-sheet with Xcode 10.2 Playground (Design-Patterns.playground.zip). 🇨🇳中文版 👷 Project started by: @nsmeme (Oktawian Chojnacki) 👷 中文版由 @binglogo (棒棒彬) 整理翻译。 🚀 How to generate README, Playground and zip from source: GENERATE.md print("Welcome!") Table of Contents Behavioral Creational Structural 🐝 Chain Of Responsibility 🌰 Abstract Factory 🔌 Adapter 👫 Command 👷 Builder 🌉 Bridge 🎶 Interpreter 🏭 Factory Method 🌿 Composite 🍫 Iterator 🔂 Monostate 🍧 Decorator 💐 Mediator 🃏 Prototype 🎁 Façade 💾 Memento 💍 Singleton 🍃 Flyweight 👓 Observer ☔ Protection Proxy 🐉 State 🍬 Virtual Proxy 💡 Strategy 🏃 Visitor 📝 Template Method Behavioral In software engineering, behavioral design patterns are design patterns that identify common communication patterns between objects and realize these patterns....

January 15, 2025

programming/iOS/api-clients

Swift API Client Strategy This document outlines a comprehensive strategy for creating an API client in Swift using async/await. The approach is designed to be modular, reusable, and easy to extend for various API endpoints. Components Endpoint Protocol: Defines the base URL and path for each API endpoint. NetworkManaging Protocol: Defines the contract for network operations. NetworkManager Class: Implements the NetworkManaging protocol to handle network requests. Error Handling: Manages errors that may occur during network requests....

January 15, 2025

SOLID

SOLID: The First 5 Principles of Object Oriented Design | DigitalOcean #weblinks Dependency Inversion Principle Entities must depend on abstractions, not on concretions. It states that the high-level module must not depend on the low-level module, but they should depend on abstractions. The Dependency Inversion Principle is a software design principle that states that high-level modules should not depend on low-level modules. Instead, both should depend on abstractions. This allows for greater flexibility and maintainability of the code....

January 15, 2025

Understanding Swift's Codable Protocol

Swift’s Codable protocol is a powerful feature that simplifies the process of encoding and decoding data between Swift types and external representations like JSON. This guide covers everything you need to know about working with Codable. What is Codable? Codable is a type alias that combines two protocols: typealias Codable = Encodable & Decodable Encodable: Allows converting Swift types to external formats (e.g., JSON) Decodable: Allows converting external formats back into Swift types Basic Usage The simplest way to use Codable is with types that have Codable-compatible properties:...

January 15, 2025

programming/iOS

Apple Challenge 1 /* A FunkyNotificationCenter object that broadcasts events within a program. Code that wants to observe notification events registers a closure via the addObserver(:for:block:) method. Code that sends notifications uses the post(notificationName:) method. Each closure that was registered for that notification name should be called. */ import Foundation class FunkyNotificationCenter { typealias notifBlock = () -> Void var entries: [String: [notifBlock]] = [:] init() // mynotif, block1, block2 /// Post a notification with the given name....

January 11, 2023