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
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
@sharplet
sharplet / LineSequence.swift
Created Nov 8, 2017
Getting a sequence of lines from a Swift string
View LineSequence.swift
import Foundation
let text = """
Hello
World
"""
extension String {
var lines: AnySequence<Substring> {
let string = self
@sharplet
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
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 {
@discardableResult
@sharplet
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
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
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 { $0.foo() }` or `sync { $0.bar() }`.
public final class Actor<Base> {
private var instance: Base
private let queue: DispatchQueue
public init(_ instance: Base, target: DispatchQueue? = nil) {
self.instance = instance
@sharplet
sharplet / trap.swift
Created Nov 23, 2015
Simple signal handling in Swift
View trap.swift
import Darwin
enum Signal: Int32 {
case HUP = 1
case INT = 2
case QUIT = 3
case ABRT = 6
case KILL = 9
case ALRM = 14
case TERM = 15
You can’t perform that action at this time.