Skip to content

Instantly share code, notes, and snippets.

@IanKeen
IanKeen / FocusState.swift
Last active June 30, 2023 17:00
SwiftUI: FocusedState shim for < iOS15
import Combine
import SwiftUI
extension View {
public func focused<T>(file: StaticString = #file, _ state: FocusState<T>, equals value: T) -> some View {
modifier(FocusedModifier(state: state, id: value, file: file))
}
}
@propertyWrapper
@IanKeen
IanKeen / View+Discover.swift
Last active February 13, 2024 08:00
SwiftUI: discover underlying components to fill in gaps in SwiftUI api
import SwiftUI
extension View {
public func discover<T: UIView>(
tag: Int = .random(in: (.min)...(.max)),
where predicate: @escaping (T) -> Bool = { _ in true },
_ closure: @escaping (T) -> Void
) -> some View {
self.overlay(
DiscoveryView(tag: tag)
@tykurtz
tykurtz / grokking_to_leetcode.md
Last active April 19, 2024 01:54
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

@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)
}
@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 / 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,
@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 / 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()
# 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 = []