Skip to content

Instantly share code, notes, and snippets.

@sharplet
sharplet / SimpleNetworkingExample.swift
Created September 14, 2017 21:37
Example Source Code for "A Simple Approach to Thread-Safe Networking in iOS Apps"
import Foundation
import PlaygroundSupport
enum URLResult {
case response(Data, URLResponse)
case error(Error, Data?, URLResponse?)
}
extension URLSession {
@discardableResult
@sharplet
sharplet / Example.swift
Created November 3, 2017 17:46
An improved wrapper for UIImagePickerController
// 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 / scalars.swift
Last active April 27, 2021 19:03
Swift program to print out all the unicode scalars in a Foundation CharacterSet.
// 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 / UIImage+Resize.swift
Last active December 20, 2020 21:41
Create an image thumbnail with an aspect fill resize mode
extension UIImage {
enum AspectOrientation {
case portrait
case square
case landscape
}
var aspectRatio: CGFloat {
return size.aspectRatio
}
@sharplet
sharplet / Channel.swift
Created August 13, 2020 13:16
A simple generic unbuffered channel using NSCondition
import Foundation
class Channel<Message> {
private enum State {
case empty
case readyToReceive
case full(Message)
}
private let condition: NSCondition
@sharplet
sharplet / KeychainItem.swift
Created April 25, 2020 23:57
A lightweight keychain wrapper for querying the keychain databse
// 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 / Example.swift
Last active May 11, 2019 13:00
Mutable property lenses for ReactiveSwift with key paths
struct User {
var name: String
}
let currentUser = MutableProperty(User(name: "Foo"))
currentUser[\.name] <~ nameTextField.reactive.textValues
@sharplet
sharplet / ContentViewEmbedding.swift
Created February 6, 2019 16:32
A protocol for view controllers with a replaceable content view controller
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 / README.md
Last active June 8, 2018 22:10
Unearned Stack Overflow badges Dashing widget

Badge Overflow

An exceptionally handsome way to track your Stack Overflow badges.

Created by Adam & Stephanie Sharp.

Unearned Badges widget

A Dashing widget that tracks your progress toward unearned badges on Stack Overflow.

@sharplet
sharplet / LineSequence.swift
Created November 8, 2017 20:03
Getting a sequence of lines from a Swift string
import Foundation
let text = """
Hello
World
"""
extension String {
var lines: AnySequence<Substring> {
let string = self