Skip to content

Instantly share code, notes, and snippets.

@smosko
smosko / UIResponder+Events.swift
Last active April 23, 2019 12:57
Bubble up events from child ViewControllers using the UIResponder chain
// LocationPickerViewController.swift
class LocationPickerViewController: UITableViewController {
...
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
didSelectLocation(locations[indexPath.row])
}
}
extension UIResponder {
@smosko
smosko / NetworkService.swift
Last active September 22, 2018 06:59
Generic NetworkService
import Foundation
public enum Result<Value> {
case success(Value)
case failure(Error)
}
public protocol Resource {
associatedtype Response
func makeRequest() -> URLRequest
@smosko
smosko / TOTP.swift
Last active March 10, 2019 06:21
Time-based OTP 2FA Token
// import HMAC in bridging header:
// #import <CommonCrypto/CommonHMAC.h>
import Foundation
enum OTPAlgorithm: String {
case sha1, sha256, sha512
var hmacAlgorithm: CCHmacAlgorithm {
switch self {
@smosko
smosko / Logger.swift
Created September 7, 2018 14:23
Simple Swift Logger
import Foundation
public final class Logger {
public enum Level: String {
case verbose = "◻️"
case debug = "◼️"
case info = "🔹"
case warning = "🔸"
case error = "🔴"
@smosko
smosko / SetupClosures.swift
Last active March 22, 2019 06:06
Setup closures
/// make { ... }
func make<T>(_ setup: (T) -> Void) -> T where T: NSObject {
let instance = T()
setup(instance)
return instance
}
let label: UILabel = make {
$0.text = "Welcome"
@smosko
smosko / OSLog+Extensions.swift
Last active December 26, 2022 07:54
Unified Logging Wrapper
import Foundation
@_exported import os.log
public extension OSLog {
convenience init(_ bundle: Bundle = .main, category: String? = nil) {
self.init(subsystem: bundle.bundleIdentifier ?? "default", category: category ?? "default")
}
convenience init(_ aClass: AnyClass, category: String? = nil) {
@smosko
smosko / Luhn.swift
Created December 25, 2018 14:50
Luhn algorithm
// https://en.wikipedia.org/wiki/Luhn_algorithm
func luhn(_ number: String) -> Bool {
return number.reversed().enumerated().map({
let digit = Int(String($0.element))!
let even = $0.offset % 2 == 0
return even ? digit : digit == 9 ? 9 : digit * 2 % 9
}).reduce(0, +) % 10 == 0
}
@smosko
smosko / StackViewController.swift
Last active October 27, 2019 16:04
Building complex screens with child ViewControllers
class StackViewController: UIViewController {
private let scrollView = UIScrollView()
private let stackView = UIStackView()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(scrollView)
scrollView.addSubview(stackView)
setupConstraints()
stackView.axis = .vertical
import Dispatch
public final class Atomic<A> {
private var _value: A
private let queue: DispatchQueue
public init(_ value: A, targetQueue: DispatchQueue? = nil) {
_value = value
queue = DispatchQueue(label: "Atomic", attributes: .concurrent, target: targetQueue)
@smosko
smosko / AsyncOperation.swift
Created March 7, 2019 09:37
AsyncOperation
import Foundation
open class AsyncOperation: Operation {
private enum State: String {
case ready = "isReady"
case executing = "isExecuting"
case finished = "isFinished"
}