Skip to content

Instantly share code, notes, and snippets.

View alexdrone's full-sized avatar

Alex Usbergo alexdrone

View GitHub Profile
@nalexn
nalexn / CancelBag.swift
Last active November 1, 2023 06:43
Collecting AnyCancellable tokens in declarative SwiftUI fashion
// Copyright © 2019 Alexey Naumov. MIT License
import Combine
typealias CancelBag = Set<AnyCancellable>
extension CancelBag {
mutating func collect(@Builder _ cancellables: () -> [AnyCancellable]) {
formUnion(cancellables())
}
@alexdrone
alexdrone / Typography.swift
Last active December 29, 2018 09:26
Customizable template used to handle app-wide typography.
import UIKit
// Example usage:
// let label = UILabel()
// label.attributedText = Typography.current.style(.button).asAttributedString("Edit"),
class Typography {
/// The current application typography.
/// Override this static property to use a custom typography.
static var current: TypographyProtocol = BaseTypography()
@alexdrone
alexdrone / Assign.swift
Created September 7, 2017 10:59
Object.assign in Swift
import Foundation
// MARK: - Function
/// This function is used to copy the values of all enumerable own properties from one or more
/// source struct to a target struct. It will return the target struct.
public func assign<T>(_ value: T, changes: (inout T) -> Void) -> T {
guard Mirror(reflecting: value).displayStyle == .struct else {
fatalError("'value' must be a struct.")
}
//: Playground - noun: a place where people can play
import Cocoa
class Observation<T>: Hashable {
var hashValue: Int {
return ObjectIdentifier(self).hashValue
}
static func ==(lhs: Observation<T>, rhs: Observation<T>) -> Bool {
@peeblesjs
peeblesjs / gist:9288f79322ed3119ece4
Last active February 11, 2016 23:31
A naive "valueForKey:" operator in Swift
operator infix --> {}
func --> (instance: Any, key: String) -> Any? {
let mirror = reflect(instance)
for index in 0 ..< mirror.count {
let (childKey, childMirror) = mirror[index]
if childKey == key {
return childMirror.value
}
}
@correia
correia / swift-kvo-example.swift
Last active April 16, 2023 02:38
A quick example for how to use Foundation style KVO from Swift. (Official documentation from Apple is forthcoming.)
//
// Swift-KVO
//
// Created by Jim Correia on 6/5/14.
// Copyright (c) 2014-2015 Jim Correia. All rights reserved.
//
// Update: 6/17/2014
//
// KVOContext has gone away; use the same idiom you'd use from Objective-C for the context
//
@mattt
mattt / uiappearance-selector.md
Last active February 7, 2025 15:27
A list of methods and properties conforming to `UIAppearance` as of iOS 12 Beta 3

Generate the list yourself:

$ cd /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS*.sdk/System/Library/Frameworks/UIKit.framework/Headers
$ grep UI_APPEARANCE_SELECTOR ./*     | \
  sed 's/NS_AVAILABLE_IOS(.*)//g'     | \
  sed 's/NS_DEPRECATED_IOS(.*)//g'    | \
  sed 's/API_AVAILABLE(.*)//g'        | \
  sed 's/API_UNAVAILABLE(.*)//g'      | \
 sed 's/UI_APPEARANCE_SELECTOR//g' | \