Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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 / 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 / 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 {