Skip to content

Instantly share code, notes, and snippets.

import UIKit
/// Object observing keyboard changes and passing a `KeyboardChangeInfo` to notify about changes.
final class KeyboardObserver: NSObject {
typealias KeyboardChangeClosure = (KeyboardChangeInfo) -> Void
let changeClosure: KeyboardChangeClosure
// MARK: - Private
@daehn
daehn / Searchable.swift
Last active April 23, 2019 03:05
Swift protocols and protocol extensions to easily add and delete conforming instances to Spotlight. Blog post can be found here: http://silvandaehn.com/2015/09/25/Simplifying-CoreSpotlight/
import UIKit
import CoreSpotlight
import MobileCoreServices
public typealias Completion = NSError? -> Void
// MARK: - Searchable Protocol
public protocol Searchable {
static var spotlightDomainIdentifier: String { get }
import Foundation
extension Equatable {
/// Whether `self` is contained in a list of other values.
/// Variadic version, see the method below for the generic implementation.
///
/// - Parameter others: The values that `self` should be checked against.
/// - Returns: Whether or not `self` is one of the provided other values.
func isOne(of others: Self...) -> Bool {
@daehn
daehn / Log.swift
Last active November 12, 2018 13:27
Lightweight logging class supporting different topics
import Foundation
final class Log: NSObject {
enum Topic: String {
case network, app
}
private enum Level: String {
case info = ""
@daehn
daehn / Reachability.swift
Created October 24, 2018 11:01
iOS Reachability Helper
import Foundation
import SystemConfiguration
extension Notification.Name {
public static let reachabilityChanged = Notification.Name(rawValue: "reachabilityChanged")
}
protocol ReachabilityObserver: class {
func reachabilityDidChange(_ reachability: Reachability)
}
@daehn
daehn / Levenshtein.swift
Last active August 28, 2017 02:19
Calculate the Levenshtein-Distance between two Strings in Swift
extension String {
subscript(index: Int) -> Character {
return self[startIndex.advancedBy(index)]
}
subscript(range: Range<Int>) -> String {
let start = startIndex.advancedBy(range.startIndex)
let end = startIndex.advancedBy(range.endIndex)
extension DispatchQueue {
/// Helper method to use when reading from a resource
/// that is being isolated by this queue.
///
/// Using this method retriving a value like this:
/// ```
/// var result: T?
/// queue.sync {
/// result = resource["key"]
import UIKit
import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true
extension Comparable {
/// Clamp the value between `low` and `high`
func clamp(_ low: Self, _ high: Self) -> Self {
public extension IteratorProtocol {
mutating public func any(_ predicate: (Element) throws -> Bool) rethrows -> Bool {
guard let current = next() else { return false }
return try predicate(current) || any(predicate)
}
mutating public func all(_ predicate: (Element) throws -> Bool) rethrows -> Bool {
guard let current = next() else { return true }
return try predicate(current) && all(predicate)
@daehn
daehn / vibration.m
Created April 3, 2015 11:04
Custom iOS vibration pattern with 100ms duration using private API
NSMutableDictionary *vibrationParameters = [[NSMutableDictionary alloc] init];
vibrationParameters[@"VibePattern"] = @[@YES, @100];
vibrationParameters[@"Intensity"] = @1;
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wimplicit-function-declaration"
AudioServicesStopSystemSound(kSystemSoundID_Vibrate);
AudioServicesPlaySystemSoundWithVibration(kSystemSoundID_Vibrate, nil, vibrationParameters);
#pragma clang diagnostic pop