Skip to content

Instantly share code, notes, and snippets.

@benjaminsnorris
benjaminsnorris / _config-collections.yml
Last active October 20, 2017 11:19
Jekyll changes to support public drafts
collections:
drafts:
output: true
@benjaminsnorris
benjaminsnorris / TextEditing.swift
Last active April 5, 2024 14:08
Live reload of text in UITextView while preserving cursor position and text selection
import UIKit
class TextEditing: UIViewController {
@IBOutlet weak var textView: UITextView?
func updateText(with newString: String?) {
guard let textView = textView, newString = newString, (diffRange, changedText) = diff(textView.text, newString) else { return }
guard let selectedRange = textView.selectedTextRange else { textView.text = newString; return }
textView.text = newString
private func nearestPotentialStartTime(toTime time: NSDate) -> NSDate {
let calendar = NSCalendar.currentCalendar()
let components = calendar.components([.Era, .Year, .Month, .Day, .Hour, .Minute], fromDate: time)
let minutes = components.minute
let nextStartMark = (minutes % startInterval) + startInterval
let previousStartMark = nextStartMark - startInterval
let previousDelta = abs(minutes - previousStartMark)
let nextDelta = abs(minutes - nextStartMark)
@benjaminsnorris
benjaminsnorris / UIColor+Hex.swift
Created May 9, 2016 21:56
Get UIColor from hex value
// MARK: - HEX init
// From Tim Shadel
enum UIColorInputError: ErrorType {
case UnableToScanHexValue
}
extension UIColor {
convenience init(hex: Int, alpha: CGFloat = 1.0) {
@benjaminsnorris
benjaminsnorris / AwesomeError.swift
Created May 9, 2016 21:53
Swift error with default description
// From Tim Shadel
enum FakeError: String, ErrorType, CustomStringConvertible {
case LoginFailed
var description: String {
return "FakeError: \(self.rawValue)"
}
}
@benjaminsnorris
benjaminsnorris / ErrorType+Describe
Created May 9, 2016 21:51
Add default text to all errors
extension ErrorType {
// MARK: - Internal functions
func describe() -> String {
if let error = self as? CustomStringConvertible {
return error.description
} else {
let error = self as NSError
return error.localizedDescription
// From Bart Whiteley
public protocol UnmarshalingFactory: Marshal.ValueType {
associatedtype ConvertibleType = Self
static func createFromObject(object: MarshaledObject) throws -> ConvertibleType
}
extension UnmarshalingFactory {
public static func value(object: Any) throws -> ConvertibleType {
@benjaminsnorris
benjaminsnorris / KeychainWrapper.swift
Created April 21, 2016 22:34
Keychain Wrapper via Dave DeLong
import Security
/// A wrapper around the Keychain API.
public class Keychain {
private let name: String
private let accessGroup: String?
public init(name: String, accessGroup: String? = nil) {
self.name = name
@benjaminsnorris
benjaminsnorris / CollectionViewController.swift
Created March 10, 2016 03:00
Snap to center collection view cell
class CollectionViewController: UIViewController {
...
func snapToCenter() {
let centerPoint = view.convertPoint(view.center, toView: collectionView)
guard let centerIndexPath = collectionView.indexPathForItemAtPoint(centerPoint)
collectionView.scrollToItemAtIndexPath(centerIndexPath, atScrollPosition: .CenteredHorizontally, animated: true)
}
...
}
struct FlagEmoji {
private static let regionalIndicatorA: UInt32 = 0x1F1E6
enum Error: ErrorType {
case ExpectedOnlyAZ
}
private static func toRegionalIndicator(scalar: UnicodeScalar) throws -> UnicodeScalar {
let offset: UInt32
switch scalar {