Skip to content

Instantly share code, notes, and snippets.

@staltz
staltz / introrx.md
Last active March 9, 2025 05:06
The introduction to Reactive Programming you've been missing
@emotality
emotality / duplicate_line_xcode.md
Last active January 24, 2025 13:49
Xcode - Duplicate Line key binding

NOTE (2022-07-09): Xcode finally added this functionality in Xcode 14, please see release notes here:

New Features in Xcode 14 Beta 3
When editing code, the Edit > Duplicate menu item and its corresponding keyboard shortcut now duplicate the selected text — or the line that currently contains the insertion point, if no text is selected. (8614499) (FB5618491)


Xcode line duplicate

Bind keys to duplicate lines in Xcode

@IanKeen
IanKeen / Lazy.swift
Last active January 9, 2024 08:41
Lazy<T>
class Lazy<T> {
private(set) var created: Bool = false
private(set) lazy var value: T = {
created = true
return builder()
}()
private var builder: () -> T
init(_ builder: @escaping () -> T) {
@IanKeen
IanKeen / BufferView.swift
Last active August 3, 2022 21:17
BufferView - useful for custom padding in a stackview
import UIKit
class BufferView<Child: UIView>: UIView {
override var layoutMargins: UIEdgeInsets {
didSet { configure() }
}
override var isHidden: Bool {
didSet { configure() }
}
@tclementdev
tclementdev / libdispatch-efficiency-tips.md
Last active March 6, 2025 20:48
Making efficient use of the libdispatch (GCD)

libdispatch efficiency tips

The libdispatch is one of the most misused API due to the way it was presented to us when it was introduced and for many years after that, and due to the confusing documentation and API. This page is a compilation of important things to know if you're going to use this library. Many references are available at the end of this document pointing to comments from Apple's very own libdispatch maintainer (Pierre Habouzit).

My take-aways are:

  • You should create very few, long-lived, well-defined queues. These queues should be seen as execution contexts in your program (gui, background work, ...) that benefit from executing in parallel. An important thing to note is that if these queues are all active at once, you will get as many threads running. In most apps, you probably do not need to create more than 3 or 4 queues.

  • Go serial first, and as you find performance bottle necks, measure why, and if concurrency helps, apply with care, always validating under system pressure. Reuse

@vinczebalazs
vinczebalazs / SheetModalPresentationController.swift
Last active July 21, 2024 15:36
A presentation controller to use for presenting a view controller modally, which can be dismissed by a pull down gesture. The presented view controller's height is also adjustable.
import UIKit
extension UIView {
var allSubviews: [UIView] {
subviews + subviews.flatMap { $0.allSubviews }
}
func firstSubview<T: UIView>(of type: T.Type) -> T? {
allSubviews.first { $0 is T } as? T