Skip to content

Instantly share code, notes, and snippets.

Adam Sharp sharplet

Block or report user

Report or block sharplet

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
sharplet / UIImage+Resize.swift
Last active Oct 3, 2019
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 / DispatchSourceInterrupt.swift
Created Jun 18, 2019
Demo of DispatchSourceSignal for handling interrupts
View DispatchSourceInterrupt.swift
import Darwin.C
import Dispatch
let source = DispatchSource.makeSignalSource(signal: SIGINT, queue: .main)
var count = 0
source.setEventHandler {
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 / 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
sharplet / LineSequence.swift
Created Nov 8, 2017
Getting a sequence of lines from a Swift string
View LineSequence.swift
import Foundation
let text = """
extension String {
var lines: AnySequence<Substring> {
let string = self
sharplet / Example.swift
Created Nov 3, 2017
An improved wrapper for UIImagePickerController
View Example.swift
// Here is a usage example. Refer to ImagePicker.swift below for the implementation!
// 1. Easily configure the picker
let cameraPicker = ImagePicker(sourceType: .camera)
let cropPicker = ImagePicker(sourceType: .photoLibrary, allowsEditing: true)
// Automatically includes both kUTTypeImage and kUTTypeLivePhoto
let livePhotoPicker = ImagePicker(sourceType: .photoLibrary, mediaTypes: [.livePhotos])
// 2. Use the picker
sharplet / SimpleNetworkingExample.swift
Created Sep 14, 2017
Example Source Code for "A Simple Approach to Thread-Safe Networking in iOS Apps"
View SimpleNetworkingExample.swift
import Foundation
import PlaygroundSupport
enum URLResult {
case response(Data, URLResponse)
case error(Error, Data?, URLResponse?)
extension URLSession {
sharplet / BitMask.swift
Created Jul 11, 2017
Playing around with a high-level API for working with bit masks
View BitMask.swift
struct BitMask<T: FixedWidthInteger>: RandomAccessCollection {
typealias SubSequence = BitMask<T>
var rawValue: T {
var rawValue: T = 0
for bit in self {
rawValue |= bit
return rawValue
sharplet / scalars.swift
Last active Apr 7, 2017
Swift program to print out all the unicode scalars in a Foundation CharacterSet.
View scalars.swift
// Prints out all the unicode scalars in a Foundation CharacterSet.
// Compile: swiftc -O scalars.swift
// Run: ./scalars <character set name>
import Foundation
extension UnicodeScalar {
static var allScalars: AnySequence<UnicodeScalar> {
let numbers = sequence(first: 0, next: { $0 + 1 })
sharplet / Actor.swift
Last active Apr 5, 2017
Simple GCD-based actors in Swift
View Actor.swift
import Dispatch
/// Wraps some `Base` type so that all method calls become
/// "message sends", e.g., `async { $ }` or `sync { $ }`.
public final class Actor<Base> {
private var instance: Base
private let queue: DispatchQueue
public init(_ instance: Base, target: DispatchQueue? = nil) {
self.instance = instance
You can’t perform that action at this time.