Skip to content

Instantly share code, notes, and snippets.

@albertbori
albertbori / CPDI-docs.md
Last active April 22, 2024 19:30
Composed Protocol Dependency Injection Documentation

Composed Protocol Dependency Injection (CPDI) Pattern

This dependency injection pattern uses native Swift language features to provide a safe, concise, deterministic, and intentional approach to dependency injection.

Overview

The primary Swift language feature that drives CPDI is called "protocol composition". This feature allows you to create a type alias from any combination of protocols. For example:

protocol Car {
@albertbori
albertbori / SwiftUI_willSet_vs_didSet_Publishers.swift
Last active February 23, 2023 18:54
SwiftUI willSet vs didSet onReceive
// Credit: Michael LaRandeau (@mlarandeau)
import SwiftUI
import Combine
import PlaygroundSupport
class ValueProvider: ObservableObject {
private let didSetSubject = CurrentValueSubject<Int, Never>(0)
@albertbori
albertbori / ExampleA.swift
Last active December 16, 2022 19:37
Swift Property Wrapper Implicit Initialization Scenarios
/*
This example shows how a simple property wrapper can be declared and initialized.
*/
@propertyWrapper
struct ExampleA<Value> {
private var value: Value
var wrappedValue: Value {
get { value }
@albertbori
albertbori / Array+Diff.swift
Last active August 9, 2018 10:13
An extension for Swift Array that returns the difference between two arrays
//: Playground - noun: a place where people can play
extension Array where Element: Comparable {
/**
Compares array with passed array and returns the differences. Warning: Must be a set of data (no duplicate values)
- Parameter with: The new array to compare to the existing array
- Returns: A tuple with an array of added items and an array of removed items.
*/
func diff(with array: Array) -> (added: Array, removed: Array) {
let originalArray = self.sorted()
@albertbori
albertbori / LocalNotifications.swift
Created July 13, 2018 23:55
NotificationCenter alternative using enums and delegates (fully swift)
import Foundation
import PlaygroundSupport
//class that allows fully-swift weak reference storage for arrays/dictionaries
class Weak<T> {
private weak var _object: AnyObject?
var object: T? { return _object as? T }
init(object: T) {
_object = object as AnyObject
@albertbori
albertbori / LocalNotifications.swift
Last active July 13, 2018 23:43
NotificationCenter alternative using delegates (fully swift)
import Foundation
import PlaygroundSupport
//class that allows fully-swift weak reference storage for arrays/dictionaries
class Weak<T> {
private weak var _object: AnyObject?
var object: T? { return _object as? T }
init(object: T) {
_object = object as AnyObject
//
// StandardTooltip.swift
//
// Created by Albert Bori on 11/20/17.
//
import Foundation
@objc
class StandardTooltip: NSObject {
@albertbori
albertbori / StackCollectionView.swift
Last active August 9, 2018 10:14
An example of a UICollectionView-like view made from UIStackViews for easy layout of small grids
//
// StackCollectionView.swift
//
// Created by Albert Bori on 6/23/17.
//
import UIKit
@IBDesignable
class StackCollectionView: UIView {
@albertbori
albertbori / README.md
Last active November 9, 2015 20:59
LINQ GroupByDate Reporting Extension

LINQ GroupByDate Reporting Extension

This IQueryable<T> extension saves a bit of time when writing a report that groups results by date. You can specify which timeframes to group by (day, week, month, year), or use the default automatic thresholds.

This method also accepts and corrects null dates based on the available results. It does this by performing a simple query to grab the starting date, if it's missing, before running the main report query.

Sample Usage

public class OrderCountRow: ReportingExtensions.ReportRow
import UIKit
import MBProgressHUD
class ImageViewController: UIViewController, UIScrollViewDelegate {
@IBOutlet weak var scrollView: UIScrollView!
var imageURL: String?
private var imageView: UIImageView!