Skip to content

Instantly share code, notes, and snippets.

@sharplet
sharplet / run.sh
Created Apr 19, 2021
Use bin/run to run Swift commands found in a project's bin/ directory
View run.sh
#!/bin/sh
#
# bin/run: Run Swift commands in a project's bin/ directory.
#
# Drop-in replacement for `swift run`.
# Adds caching of built products based on source file contents.
#
# Usage:
# bin/run <command [options]> # Run named command defined in `bin/Package.swift`
#
@sharplet
sharplet / AnyEventPublisher.swift
Last active Jan 22, 2021
PublisherQueue — Serialise the execution of multiple publishers like OperationQueue
View AnyEventPublisher.swift
import Combine
public enum AnyError: Error {
case never(Never)
case failure(Error)
}
public enum AnyEvent {
case output(Any)
case completion(Subscribers.Completion<AnyError>)
@sharplet
sharplet / Channel.swift
Created Aug 13, 2020
A simple generic unbuffered channel using NSCondition
View Channel.swift
import Foundation
class Channel<Message> {
private enum State {
case empty
case readyToReceive
case full(Message)
}
private let condition: NSCondition
@sharplet
sharplet / Always.swift
Last active Apr 27, 2021
A Combine publisher that repeatedly emits the same value as long as there is demand
View Always.swift
// Copyright (c) 2019–20 Adam Sharp and thoughtbot, inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
@sharplet
sharplet / KeychainItem.swift
Created Apr 25, 2020
A lightweight keychain wrapper for querying the keychain databse
View KeychainItem.swift
// Copyright (c) 2019–20 Adam Sharp and thoughtbot, inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
View CurrentValuePublisher.swift
// Copyright (c) 2019–20 Adam Sharp and thoughtbot, inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
@sharplet
sharplet / UIImage+Resize.swift
Last active Dec 20, 2020
Create an image thumbnail with an aspect fill resize mode
View UIImage+Resize.swift
extension UIImage {
enum AspectOrientation {
case portrait
case square
case landscape
}
var aspectRatio: CGFloat {
return size.aspectRatio
}
@sharplet
sharplet / DispatchSourceInterrupt.swift
Created Jun 18, 2019
Demo of DispatchSourceSignal for handling interrupts
View DispatchSourceInterrupt.swift
import Darwin.C
import Dispatch
signal(SIGINT, SIG_IGN)
let source = DispatchSource.makeSignalSource(signal: SIGINT, queue: .main)
var count = 0
source.setEventHandler {
@sharplet
sharplet / ContentViewEmbedding.swift
Created Feb 6, 2019
A protocol for view controllers with a replaceable content view controller
View ContentViewEmbedding.swift
import UIKit
/// For a view controller that has no content of its own, implement like so:
///
/// extension RootViewController: ContentViewEmbedding {
/// var contentView: UIView! {
/// return view
/// }
/// }
protocol ContentViewEmbedding {
@sharplet
sharplet / Example.swift
Last active May 11, 2019
Mutable property lenses for ReactiveSwift with key paths
View Example.swift
struct User {
var name: String
}
let currentUser = MutableProperty(User(name: "Foo"))
currentUser[\.name] <~ nameTextField.reactive.textValues