Skip to content

Instantly share code, notes, and snippets.

View iby's full-sized avatar
👻

Ian Bytchek iby

👻
View GitHub Profile
@iby
iby / ISA-swizzling and breaking KVO.md
Last active August 8, 2024 11:55
Research notes and playground for ISA-swizzling breaking KVO and causing exceptions during observer removal / observee deinitialization when using ReactiveCocoa on macOS 10.15 and above, see full discussion: https://github.com/ReactiveCocoa/ReactiveCocoa/issues/3690

This experiment was born out of ReactiveCocoa#3690 – a homeopathy to fix swizzling with more swizzling.

Ideally, there would be a way to simply intercept the call with a custom handler to add some logic. However, that's far from simple:

  • Need to handle existing class methods (add vs. replace).
  • Need to call original implementation and pass it parameters – no way of doing it in Swift in some cases (via message forwarding), no way of doing it reliably in Objective-C in others (via va_list)…

In theory, where my research stops, it's possible to achieve this via scenario:

  1. Add a block implementation under a random non-clashing selector.
@iby
iby / FileDescriptor.swift
Last active July 10, 2024 07:30
At attempt to better understand Darwin's standard streams (stdout, stderr) and file descriptors.
import Foundation
fileprivate enum StdStream {
case out
case err
fileprivate var fileDescriptor: FileDescriptor {
switch self {
case .out: return FileDescriptor(STDOUT_FILENO, "stdout")
case .err: return FileDescriptor(STDERR_FILENO, "stderr")
// This is a hideous hack for 10.11 where constraints are applied for detached view and the entire
// toolset panel looks like a huge cock…
if #available(macOS 10.13, *) {
} else if self.instructionView.isHidden && self.instructionView.superview != nil {
self.stackView.removeView(self.instructionView)
} else if !self.instructionView.isHidden && self.instructionView.superview == nil {
self.stackView.insertView(self.instructionView, at: 0, in: .center)
}
@iby
iby / Exclude.swift
Created September 5, 2021 09:22
Create a list of excluded paths at the base location keeping the specified paths.
private func exclude(paths: [String], at basePath: String) -> [String] {
let slash = CharacterSet(charactersIn: "/")
let baseURL = URL(fileURLWithPath: basePath, isDirectory: true)
var exclusions = try! FileManager.default.contentsOfDirectory(atPath: basePath).sorted()
var inclusions = paths
// Cleanup paths removing and expanding `/.`, `/..`, etc.
inclusions = inclusions.reduce(into: [], { inclusions, inclusion in
let path = URL(fileURLWithPath: inclusion, relativeTo: baseURL).standardized.relativePath.trimmingCharacters(in: slash)
if !path.isEmpty { inclusions.append(path) }
@iby
iby / main.swift
Created June 17, 2019 14:27
Slang Example
import Slang
let source: String = "import Foundation; class Foo { let bar = 1 }"
let file: File = File(source)
let disassembly: Disassembly = try! Disassembly(file)
var edits: [Edit] = []
// Change "Foundation" identifier to "AppKit".
edits.append(Edit(disassembly.query.syntax.first(of: .identifier).select(where: { $0.contents == "Foundation" }).one!, "AppKit"))
// Change "class" keyword to "struct".
@iby
iby / CALayer+CARenderer.swift
Last active June 6, 2023 12:20
Rendering animated CALayer off-screen using CARenderer with MTLTexture, https://stackoverflow.com/q/56150363/458356
import AppKit
import Metal
import QuartzCore
let view = NSView(frame: CGRect(x: 0, y: 0, width: 600, height: 400))
let circle = NSView(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
circle.wantsLayer = true
circle.layer?.backgroundColor = NSColor.red.cgColor
circle.layer?.cornerRadius = 25
let fooOne: Foo = Foo()
let fooTwo: Foo = Foo()
fooOne.reactive.signal.bar.observeValues({ Swift.print($0) })
fooOne.reactive.bind.bar <~ fooTwo.reactive.signal.bar
fooOne.bar = "Such b-a-r!"
fooTwo.bar = "Very BAR!"
class Foo: ReactiveExtensionsProvider {
let pipe = Signal<String, NoError>.pipe()
var bar: String = "Such bar!" {
didSet { self.pipe.input.send(value: self.bar) }
}
}
extension Reactive.Binds where Base: Foo {
var bar: BindingTarget<String> {
return self.reactive.makeBindingTarget({ $0.bar = $1 })
extension Reactive {
/// A proxy which holds reactive binding target extensions of `Base`.
struct Binds<Base> {
fileprivate init(_ base: Base) { self.base = base }
let base: Base
}
var bind: Binds<Base> { return Binds(self.base) }
@iby
iby / MLWAsyncAVPlayer.h
Created March 11, 2018 15:48 — forked from k06a/MLWAsyncAVPlayer.h
Awesome optimized AVPlayer for smooth scrolling AVPlayerLayer inside UICollectionView/UITableView (tested on iOS10+)
#import <AVFoundation/AVFoundation.h>
@interface MLWAsyncAVPlayer : AVPlayer
@end