Skip to content

Instantly share code, notes, and snippets.

import UIKit
indirect enum Tree<Element : Comparable> {
case Empty
case Node(Element, Tree<Element>, Tree<Element>)
init() {
self = .Empty
}
extension SharedModel {
class func decode(data: Data) -> SharedModel? {
NSKeyedUnarchiver.setClass(SharedModel.self, forClassName: "SharedModel")
return NSKeyedUnarchiver.unarchiveObject(with: data) as? SharedModel
}
func encode() -> Data {
NSKeyedArchiver.setClassName("SharedModel", for: SharedModel.self)
return NSKeyedArchiver.archivedData(withRootObject: self)
import Foundation
import RxSwift
extension ObservableType where E == Bool {
func negate() -> Observable<Bool> {
return map(!)
}
}
// Neat way of avoiding if lets/guard lets
extension Optional {
func then(_ handler:(Wrapped) -> Void) {
switch self {
case .some(let value): handler(value)
case .none: break
}
}
protocol ViewModelType {
associatedtype Model
associatedtype Input
init(model: Model, input: Input)
}
class ViewModel: ViewModelType {
required init(model: Model, input: Input) {
protocol ViewModelType {
associatedtype Model
associatedtype Input
init(model: Model, input: Input)
}
class ViewModel: ViewModelType {
import RxSwift
import Moya
import Unbox
public extension ObservableType where E == Response {
public func mapObject<T: Unboxable>(type: T.Type) -> Observable<T> {
return flatMap { response -> Observable<T> in
return Observable.just(try response.mapObject(type: T.self))
}
}
import UIKit
class PixelWidthConstraint: NSLayoutConstraint {
override func awakeFromNib() {
super.awakeFromNib()
constant = 1.0 / UIScreen.main.scale
}
}
class AdjustableVisualEffectView: UIVisualEffectView {
private var animator: UIViewPropertyAnimator!
init(effect: UIVisualEffect?, intensity: CGFloat) {
super.init(effect: nil)
self.animator = UIViewPropertyAnimator(duration: 1, curve: .linear) { [weak self] in
self?.effect = effect
}
self.intensity = intensity
import UIKit
import Combine
protocol DiffableListDataSourceType {
associatedtype SectionIdentifier: Hashable
associatedtype ItemIdentifier: Hashable
func apply(_ snapshot: NSDiffableDataSourceSnapshot<SectionIdentifier, ItemIdentifier>,
animatingDifferences: Bool,
completion: (() -> Void)?)