Skip to content

Instantly share code, notes, and snippets.

Adam Sharp sharplet

View GitHub Profile
@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 Mar 2, 2018
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
@sharplet
sharplet / GreetingFeature.swift
Created Oct 25, 2015
Cucumber inspired syntax for Xcode UI tests
View GreetingFeature.swift
final class GreetingFeature: Feature {
override func scenarios() {
Scenario("Greeting on first load")
.Given("the app has launched")
.Then("the text 'Hello, world!' is on screen")
}
}
You can’t perform that action at this time.