Skip to content

Instantly share code, notes, and snippets.

Avatar
👻

Ian Bytchek ianbytchek

👻
View GitHub Profile
View main.swift
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".
@ianbytchek
ianbytchek / CALayer+CARenderer.swift
Last active Sep 9, 2020
Rendering animated CALayer off-screen using CARenderer with MTLTexture, https://stackoverflow.com/q/56150363/458356
View CALayer+CARenderer.swift
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
View ReactiveSwiftExtensionsUse.swift
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!"
View ReactiveSwiftExtensionsExample.swift
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 })
View ReactiveSwiftExtensions.swift
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) }
@ianbytchek
ianbytchek / MLWAsyncAVPlayer.h
Created Mar 11, 2018 — forked from k06a/MLWAsyncAVPlayer.h
Awesome optimized AVPlayer for smooth scrolling AVPlayerLayer inside UICollectionView/UITableView (tested on iOS10+)
View MLWAsyncAVPlayer.h
#import <AVFoundation/AVFoundation.h>
@interface MLWAsyncAVPlayer : AVPlayer
@end
@ianbytchek
ianbytchek / Foo.m
Last active May 4, 2019
Using NSObject’s +load and +initialize from Swift, checkout the full story – https://medium.com/post-mortem/using-nsobjects-load-and-initialize-from-swift-f6f2c6d9aad0
View Foo.m
#import <Foundation/Foundation.h>
#import <MyProject/MyProject-Swift.h>
// This was a more concise solution in Swift 4.2 and earlier. If you try this with Swift 5 you'd get "Swift class
// extensions and categories on Swift classes are not allowed to have +load methods" runtime error.
// @implementation Foo (private)
// + (void)load { [self swiftyLoad]; }
// + (void)initialize { [self swiftyInitialize]; }
// @end
@ianbytchek
ianbytchek / NSViewOverlapTest.swift
Last active Jul 19, 2017
Overlap test for NSView with tracking areas
View NSViewOverlapTest.swift
import AppKit
extension NSView
{
/// Checks if view overlaps with other views above it at specified **window** location.
func overlapTest(_ point: NSPoint) -> Bool {
// Perform hit test to get the view under mouse. Because it can be a view within
View gist:66f7d14bc0d4e97f12ee5aa4b52db66d
ACTION = build
AD_HOC_CODE_SIGNING_ALLOWED = NO
ALTERNATE_GROUP = staff
ALTERNATE_MODE = u+w,go-w,a+rX
ALTERNATE_OWNER = grantdavis
ALWAYS_SEARCH_USER_PATHS = NO
ALWAYS_USE_SEPARATE_HEADERMAPS = YES
APPLE_INTERNAL_DEVELOPER_DIR = /AppleInternal/Developer
APPLE_INTERNAL_DIR = /AppleInternal
APPLE_INTERNAL_DOCUMENTATION_DIR = /AppleInternal/Documentation
@ianbytchek
ianbytchek / Playground.swift
Last active Jun 29, 2020
Block / method / function comparison in Swift
View Playground.swift
import Foundation
// Some interesting observations on block comparing. Apparently swift nor objective c don't allow comparing closures
// due to the complexity of the process and because of compiler optimisations… bla bla. But it seems to be possible
// to do this using conventions blocks, aka the objective c ones that can be cast as objects. The interesting part
// is how swift freely casts SwfBlock to ObjBlock, yet in reality two casted SwfBlock blocks will always be different
// values, while ObjBlocks won't. When we cast ObjBlock to SwfBlock, the same thing happens to them, they become two
// different values. So, in order to preserve the reference, this sort of casting should be avoided.
typealias SwfBlock = () -> ()
You can’t perform that action at this time.