Skip to content

Instantly share code, notes, and snippets.

struct Person {
let name: String
let age: Int
}
struct Payment {
let currency: String
let amount: Double
}
import Foundation
// MARK: - typed predicate types
public protocol TypedPredicateProtocol: NSPredicate { associatedtype Root }
public final class CompoundPredicate<Root>: NSCompoundPredicate, TypedPredicateProtocol {}
public final class ComparisonPredicate<Root>: NSComparisonPredicate, TypedPredicateProtocol {}
// MARK: - compound operators
class GenericCache<H: Hashable, V: AnyObject> {
let nsCache = NSCache<AnyObject, V>()
subscript(_ h: H) -> V? {
get {
return nsCache.object(forKey: h as AnyObject)
}
set {
if let v = newValue {
nsCache.setObject(v, forKey: h as AnyObject)
} else {
extension FetchedData {
subscript(_ indexPath: IndexPath) -> T {
return controller.object(at: indexPath)
}
}
class FetchedData<T: NSFetchRequestResult> {
let controller: NSFetchedResultsController<T>
init(_ controller: NSFetchedResultsController<T>) {
self.controller = controller
}
}
extension NSFetchedResultsController {
@objc func entity(at row: Int) -> ResultType {
object(at: IndexPath(item: row, section: 0))
}
}
extension NSFetchedResultsController {
var sectionCount: Int {
return sections?.count ?? 0
}
}
extension NSManagedObjectContext {
func performTask(_ block: @escaping (NSManagedObjectContext) -> Void) {
perform { [unowned self] in
block(self)
}
}
}
import CoreData
public final class CoreDataStack {
public let persistentContainer: NSPersistentContainer
public init(_ persistentContainer: NSPersistentContainer) {
self.persistentContainer = persistentContainer
}
public private(set) lazy var mainContext: NSManagedObjectContext = {
@sisoje
sisoje / DeinitBlock+Timer.swift
Last active February 1, 2019 13:47
Resource acquisition is initialization (RAII) in Swift: Example
extension Timer {
var invalidation: DeinitBlock {
return DeinitBlock { [weak self] in
self?.invalidate()
}
}
}