Skip to content

Instantly share code, notes, and snippets.

View Sorix's full-sized avatar

Vasily Ulianov Sorix

View GitHub Profile
@Sorix
Sorix / SingleFetchedResultController.swift
Last active January 13, 2018 12:46
NSFetchedResultsController for single object
import CoreData
protocol SingleFetchedResultsControllerDelegate: class {
func controller(didChange anObject: NSFetchRequestResult, for type: SingleFetchedResultsChangeType)
func controller(error: SingleFetchedResultsControllerError)
}
extension SingleFetchedResultsControllerDelegate {
func controller(_ controller: SingleFetchedResultsController<NSFetchRequestResult>, error: SingleFetchedResultsControllerError) { }
}
@Sorix
Sorix / Package.swift
Last active December 23, 2023 14:26
Example of Package.swift with environment variables support
// swift-tools-version:4.0
import PackageDescription
#if os(Linux)
import Glibc
#else
import Darwin.C
#endif
enum Enviroment: String {
@Sorix
Sorix / safeTest.swift
Last active July 5, 2019 21:31
Array performance test for safe and normal get
import Foundation
import XCTest
extension Collection where Indices.Iterator.Element == Index {
subscript(safe index: Index) -> Iterator.Element? {
return indices.contains(index) ? self[index] : nil
}
}
class PerfomanceTester: XCTestCase {
@Sorix
Sorix / CurrentValue.swift
Created October 27, 2019 12:02
Basic publisher implementation to non-combine code
import Foundation
/// An object that wraps a single value and publishes a new element whenever the value changes.
@propertyWrapper struct CurrentValue<Output> {
private let publisher = BasicPublisher()
var value: Output {
didSet {
publisher.handler?(oldValue, wrappedValue)
}
if let selectedIndexPath = methodsTableView?.indexPathForSelectedRow {
if let coordinator = transitionCoordinator {
coordinator.animate(alongsideTransition: { context in
self.methodsTableView?.deselectRow(at: selectedIndexPath, animated: true)
}) { context in
if context.isCancelled {
self.methodsTableView?.selectRow(at: selectedIndexPath, animated: false, scrollPosition: .none)
}
}
} else {
@Sorix
Sorix / customEnumerationDecoding.swift
Last active April 20, 2023 10:43
snake_case to camelCase Swift Decodable
import UIKit
enum InteractionCode: String, Decodable {
case PROCEED, abort, tryOtherNetwork, TRY_OTHER_ACCOUNT, RETRY, RELOAD, VERIFY
init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
let jsonString = try container.decode(String.self)
var camelCasedString = String()