Skip to content

Instantly share code, notes, and snippets.

@shawnthroop
shawnthroop / PRSModalWebViewController.h
Created January 5, 2015 01:35
A self contained UINavigationViewController and UIViewController for presenting a WKWebView on iOS 8
#import <WebKit/WebKit.h>
@protocol PRSProgressDelegate <NSObject>
- (void)setNavigationTitle:(NSString *)title;
- (void)setProgress:(CGFloat)progress animated:(BOOL)animated;
- (void)setVisible:(BOOL)visible animated:(BOOL)animated;
@end
@shawnthroop
shawnthroop / NSRange+SurrogatePairAdjustable.swift
Last active April 18, 2018 15:26
Adjusting ranges retrieved from an API (App.net, pnut.io, etc.) for use with NSAttributedString
// Swift 4.0.3
import Foundation
extension NSRange: SurrogatePairAdjustable {
func adjusted(for pairs: [String.SurrogatePair]) -> NSRange {
if pairs.isEmpty {
return self
}
@shawnthroop
shawnthroop / UIColor+Additions.swift
Last active May 29, 2017 12:22
Additions to UIColor that allow for easier manipulation of RGBA and HSBA values, in Swift
import UIKit
extension UIColor {
struct HSBA {
var hue: CGFloat
var saturation: CGFloat
var brightness: CGFloat
var alpha: CGFloat
@shawnthroop
shawnthroop / List.swift
Created March 18, 2018 17:25
List and OptimizedList, a sectioned Collection.
struct List<Element>: Sequence, Collection where Element: Hashable {
private var contents: [[Element]]
init(_ sections: [[Element]] = [[]]) {
contents = sections
}
// MARK: - Sequence
@shawnthroop
shawnthroop / Raw.swift
Last active May 14, 2018 09:42
Possible typesafe keys and values for representing pnut.io's Raw data
import Foundation
let data = """
{
"version": "1.0",
"type": "photo",
"width": 240,
"height": 160,
"title": "ZB8T0193",
"url": "http://farm4.static.flickr.com/3123/2341623661_7c99f48bbf_m.jpg",
@shawnthroop
shawnthroop / AnyEquatable.swift
Created May 16, 2018 00:23
A protocol allowing comparison between untyped values
// swift 4.1
protocol AnyEquatable {
func isEqual(to value: Any) -> Bool
}
extension AnyEquatable where Self: Equatable {
func isEqual(to value: Any) -> Bool {
guard let value = value as? Self else {
return false
@shawnthroop
shawnthroop / AnyEncodable.swift
Last active May 28, 2018 11:49
Type-erased Equatable and Encodable values
// swift 4.1
/// A type-erased encodable value
public struct AnyEncodable {
public var base: Any {
return value.base
}
private let value: AnyEquatable
private let encodeValue: (Encoder, AnyEquatable) throws -> Void
@shawnthroop
shawnthroop / Accounts.swift
Last active July 2, 2018 14:35
An elaboration of Swift Talk S01E63 (Mutable Shared Structs). Provides a simpler base class for read-only properties.
// swift 4.1
import Foundation
import Sting
struct Account: Equatable {
var id: Int
}
struct Accounts {
@shawnthroop
shawnthroop / Endpoint.swift
Last active March 12, 2019 08:18
A simple and extensible networking library. Supports posting basic multipart form data and type safe query parameters.
// swift 4.2
public struct Endpoint: Equatable {
public typealias Query = Set<QueryItem>
public typealias Headers = [HeaderField: String]
public enum Method: Equatable {
case get
case post(Body)
// swift 4.2
public class MutableVariable<A>: Variable<A> {
public typealias Transform = (inout A) -> Void
internal typealias Mutate = (@escaping Transform) -> Void
override public class func threadSafe(_ initial: A) -> MutableVariable<A> {
return MutableVariable(initial, queue: DispatchQueue(label: "com.shawnthroop.sting.\(self)", attributes: .concurrent))
}