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:...
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”) } ....
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") {} ....
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....
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....
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....
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....
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:...
Access Control
Manage the visibility of code by declaration, file, and module. Access control restricts access to parts of your code from code in other source files and modules. This feature enables you to hide the implementation details of your code, and to specify a preferred interface through which that code can be accessed and used. You can assign specific access levels to individual types (classes, structures, and enumerations), as well as to properties, methods, initializers, and subscripts belonging to those types....
Advanced Operators
Advanced Operators Define custom operators, perform bitwise operations, and use builder syntax. In addition to the operators described in doc:BasicOperators, Swift provides several advanced operators that perform more complex value manipulation. These include all of the bitwise and bit shifting operators you will be familiar with from C and Objective-C. Unlike arithmetic operators in C, arithmetic operators in Swift don’t overflow by default. Overflow behavior is trapped and reported as an error....