Skip to content

Instantly share code, notes, and snippets.

@rnapier
rnapier / json.swift
Last active January 31, 2024 12:49
Generic JSON Decodable
import Foundation
@dynamicMemberLookup
enum JSON: Codable, CustomStringConvertible {
var description: String {
switch self {
case .string(let string): return "\"\(string)\""
case .number(let double):
if let int = Int(exactly: double) {
return "\(int)"
@IanKeen
IanKeen / ControlEventBindable.swift
Last active June 4, 2022 23:18
Easy closure based access to UIControl events (instead of target/action)
protocol ControlEventBindable: class { }
extension UIControl: ControlEventBindable { }
extension UIBarButtonItem: ControlEventBindable { }
private struct Keys {
static var EventHandlers = "_EventHandlers"
}
// MARK: - Implementation
anonymous
anonymous / appnap.swift
Created March 3, 2018 21:59
import AppKit
// https://stackoverflow.com/a/48486247
func shell(_ launchPath: String, _ arguments: [String]) -> String {
let task = Process()
task.launchPath = launchPath
task.arguments = arguments
let pipe = Pipe()
task.standardOutput = pipe
# When using RN in combination with Cocoapods, a lot of
# things are broken. These are the fixes we had to append
# to our Podfile when upgrading to ReactNative@0.55.3.
#
# WARNING: Check those line numbers when you're on a different version!
def change_lines_in_file(file_path, &change)
print "Fixing #{file_path}...\n"
contents = []
@IanKeen
IanKeen / AnyCodable.swift
Last active March 4, 2021 18:45
AnyCodable
public struct AnyCodable: Codable {
public let value: Any?
public init(_ value: Any?) {
self.value = value
}
public init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
@mattt
mattt / UIViewControllerPreview.swift
Last active January 8, 2024 23:09
Generic structures to host previews of UIView and UIViewController subclasses.
import UIKit
#if canImport(SwiftUI) && DEBUG
import SwiftUI
struct UIViewControllerPreview<ViewController: UIViewController>: UIViewControllerRepresentable {
let viewController: ViewController
init(_ builder: @escaping () -> ViewController) {
viewController = builder()
}
@IanKeen
IanKeen / Redraw.swift
Last active February 28, 2023 21:20
PropertyWrapper: Automatically redraw UIViews when a value changes
@propertyWrapper
struct Redraw<Value> {
var wrappedValue: Value
init(wrappedValue: Value) {
self.wrappedValue = wrappedValue
}
static subscript<Instance: UIView>(
_enclosingInstance instance: Instance,
@IanKeen
IanKeen / Example.swift
Last active November 12, 2022 20:05
PropertyWrapper: Add `indirect` functionality to structs (w/ COW)
struct Value {
var foo: Int
@Indirect var inner: Value?
}
let x = Value(foo: 1, inner: .init(foo: 2, inner: .init(foo: 3, inner: nil)))
x.inner?.inner?.foo // 3
@IanKeen
IanKeen / AppStorage.swift
Created September 10, 2020 22:47
PropertyWrapper: AppStorage backport
@propertyWrapper
struct AppStorage<Value>: DynamicProperty {
let key: String
let defaultValue: Value
init(wrappedValue defaultValue: Value, _ key: String) {
self.key = key
self.defaultValue = defaultValue
self._wrappedValue = State(wrappedValue: UserDefaults.standard.object(forKey: key) as? Value ?? defaultValue)
}
@tykurtz
tykurtz / grokking_to_leetcode.md
Last active May 4, 2024 14:32
Grokking the coding interview equivalent leetcode problems

GROKKING NOTES

I liked the way Grokking the coding interview organized problems into learnable patterns. However, the course is expensive and the majority of the time the problems are copy-pasted from leetcode. As the explanations on leetcode are usually just as good, the course really boils down to being a glorified curated list of leetcode problems.

So below I made a list of leetcode problems that are as close to grokking problems as possible.

Pattern: Sliding Window