Skip to content

Instantly share code, notes, and snippets.

View doozMen's full-sized avatar

Stijn Willems doozMen

View GitHub Profile
/// This code defines an init method for the Binding type. This method accepts three parameters:
///
/// An object of any type T
/// A key path to a property of type Value on that object
/// An optional UndoManager object
///
/// The init method sets the Binding value to the value of the property specified by the key path. It also sets the set value of the Binding to a closure that updates the value of the property at the key path and registers an undo operation with the provided UndoManager, if one is given.
///
/// This allows the Binding object to be used to access and update the value of the specified property on the provided object, and to register undo operations for those updates with the UndoManager.
@aibo-cora
aibo-cora / Converter.swift
Created July 17, 2021 19:15
Class to convert AVAudioPCMBuffer to CMSampleBuffer.
import Foundation
import AVFoundation
import CoreMedia
class Converter {
static func configureSampleBuffer(pcmBuffer: AVAudioPCMBuffer) -> CMSampleBuffer? {
let audioBufferList = pcmBuffer.mutableAudioBufferList
let asbd = pcmBuffer.format.streamDescription
var sampleBuffer: CMSampleBuffer? = nil
// Advanced SwiftUI Transitions
// https://swiftui-lab.com
// https://swiftui-lab.com/advanced-transitions
import SwiftUI
struct CrossEffectDemo: View {
let animationDuration: Double = 2
let images = ["photo1", "photo2", "photo3", "photo4"]
@State private var idx = 0
@backslash-f
backslash-f / README.md
Last active November 19, 2023 15:59
Sample on adding repo images and shields.io badges on README.md files

Image that is in the repo:

![Key Mapping](https://github.com/backslash-f/ShockEmu/blob/master/KeyMapping.png)

Or by path:

![Storyboard](Images/xcode-storyboard.png)
@sharplet
sharplet / DispatchSourceInterrupt.swift
Created June 18, 2019 14:14
Demo of DispatchSourceSignal for handling interrupts
import Darwin.C
import Dispatch
signal(SIGINT, SIG_IGN)
let source = DispatchSource.makeSignalSource(signal: SIGINT, queue: .main)
var count = 0
source.setEventHandler {
import UIKit
import AVFoundation
var tb = mach_timebase_info() // イニシャライザ
mach_timebase_info(&tb) // こっちは関数呼び出し
let tsc = mach_absolute_time()
let t = Double(tsc) * Double(tb.numer) / Double(tb.denom) / 1000000000.0
let seconds = AVAudioTime.seconds(forHostTime: tsc)
let cmtime = CMTime(seconds: seconds, preferredTimescale: 1000000000)
@4np
4np / HowTo use xcconfig or plist with SPM.md
Last active June 18, 2024 15:12
How to use a .xcconfig file and a .plist with a Swift Package Manager based project.

How to use a .xcconfig file and a .plist file with SPM

Worth a read for some more context.

Create a Package.xcconfig file

Create the file in the root of the project (where your Package.swift file lives as well), and use the following contents:

/// Package.xcconfig
@Sorix
Sorix / Package.swift
Last active December 23, 2023 14:26
Example of Package.swift with environment variables support
// swift-tools-version:4.0
import PackageDescription
#if os(Linux)
import Glibc
#else
import Darwin.C
#endif
enum Enviroment: String {
@algal
algal / PrintToStdErr.swift
Last active July 27, 2022 06:47
print to stderr in Swift 3
// known-good: Xcode 8, Swift 3
import Foundation
var standardError = FileHandle.standardError
extension FileHandle : TextOutputStream {
public func write(_ string: String) {
guard let data = string.data(using: .utf8) else { return }
@andymatuschak
andymatuschak / States-v3.md
Last active June 12, 2024 04:17
A composable pattern for pure state machines with effects (draft v3)

A composable pattern for pure state machines with effects

State machines are everywhere in interactive systems, but they're rarely defined clearly and explicitly. Given some big blob of code including implicit state machines, which transitions are possible and under what conditions? What effects take place on what transitions?

There are existing design patterns for state machines, but all the patterns I've seen complect side effects with the structure of the state machine itself. Instances of these patterns are difficult to test without mocking, and they end up with more dependencies. Worse, the classic patterns compose poorly: hierarchical state machines are typically not straightforward extensions. The functional programming world has solutions, but they don't transpose neatly enough to be broadly usable in mainstream languages.

Here I present a composable pattern for pure state machiness with effects,