This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This is a variation on @merowing_'s Versionable: https://github.com/krzysztofzablocki/Versionable | |
// It's an attempt to add enumerated versions. Together with CaseIterable conformance, this makes | |
// it harder to "forget" to add migrations for version bumps. Not super happy with the namings, but it | |
// should be enough to get the idea. | |
// | |
// Modifications made: | |
// - New protocol `VersionType` | |
// - Versionable.Version is `VersionType` | |
// - Replaced Versionable.migrations with Versionable.migrate(to:) | |
// - decode() fetches migrations via Versionable.Version.allCases and Versionable.migrate(to:) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import PlaygroundSupport | |
import UIKit | |
class M { | |
var a = "" | |
func x() { self.a = "A" } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diskutil secureErase freespace 0 /dev/disk1s1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension Date { | |
var timeAgo: String { | |
let calendar = Calendar.current | |
let now = Date() | |
let earliest = self < now ? self : now | |
let latest = self > now ? self : now | |
let unitFlags: Set<Calendar.Component> = [.minute, .hour, .day, .weekOfMonth, .month, .year, .second] | |
let components: DateComponents = calendar.dateComponents(unitFlags, from: earliest, to: latest) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import UIKit | |
class CachedItemHeightDatasource: NSObject, UITableViewDelegate, UITableViewDataSource { | |
var heightAtIndexPath: [IndexPath: CGFloat] = [:] | |
let tableView: UITableView | |
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat { | |
return heightAtIndexPath[indexPath] ?? UITableViewAutomaticDimension |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
/// Thread-safe value wrapper with asynchronous setter and | |
/// synchronous getter. | |
/// | |
/// Some discussion: https://twitter.com/manuelmaly/status/1077885584939630593?s=20 | |
public final class SynchronizedProperty<T> { | |
public typealias DidSet = Bool | |
public let executer: SynchronizedExecuter |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public protocol TableViewCellDequeueIdentifier: CustomStringConvertible { } | |
public enum TableViewCellProducer<UITVC: UITableViewCell>: Equatable { | |
case classAndIdentifier(class: UITVC.Type, identifier: TableViewCellDequeueIdentifier, configure: (UITVC) -> ()) | |
case generator(() -> UITVC) | |
func cell(tableView: UITableView) -> UITVC { | |
switch self { | |
case let .classAndIdentifier(clazz, identifier, configure): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import UIKit | |
// Uses locking from https://gist.github.com/einsteinx2/00b9ebd962f3a0f6c9e758f842e4c6f9 | |
class HTMLAttributedStringCache { | |
private let lock = NSLock() | |
private struct Key: Hashable { | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// mark - CDEPersistentStoreEnsembleDelegate | |
public func persistentStoreEnsemble(_ ensemble: CDEPersistentStoreEnsemble, didSaveMergeChangesWith notification: Notification) { | |
func mergeChanges(stack: DataStack) { | |
assert(Thread.isMainThread) | |
let mainContext = stack.internalContext() | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
extension Array where Element:Equatable { | |
typealias ArrayDiffResult = (added: [(Index, Element)], removed: [(Index, Element)], changed: [(Index, Element)]) | |
// Inspired by: http://stackoverflow.com/q/3476672/458603 | |
// compare has to return .orderedSame if the identity of the element is the same, | |
// even if the element's values have changed. | |
func diff(other: Array<Element>, compare: ((Element, Element) -> ComparisonResult), preSorted: Bool) -> ArrayDiffResult { | |