Skip to content

Instantly share code, notes, and snippets.

@iThinker
iThinker / observable_v2.swift
Created November 1, 2016 00:44
Observable V2
import Foundation
protocol Observable: class {
associatedtype ObservableAction
typealias SubscriptionBlock = (ObservableAction) -> Void
var subscriptions: NSMapTable<AnyObject, AnyObject> { get set }
func notifyObservers(_ action: ObservableAction) -> Void
@iThinker
iThinker / observable_v3.swift
Created November 1, 2016 01:30
Observable V3
import Foundation
class ObserverStorage {
static let shared = ObserverStorage()
private var observablesMap: NSMapTable<AnyObject, AnyObject> = NSMapTable.weakToStrongObjects()
private func subscriptionsMap<T: Observable>(forObservable observable: T) -> NSMapTable<AnyObject, AnyObject> {
var subscriptionsMap = self.observablesMap.object(forKey: observable) as? NSMapTable<AnyObject, AnyObject>
@iThinker
iThinker / observable_v4.swift
Created November 1, 2016 14:25
Observable V4
//
// Observable.swift
// Geotification
//
// Created by Roman Temchenko on 2016-10-29.
// Copyright © 2016 Temkos. All rights reserved.
//
import Foundation
import Foundation
class AssociationsStorage {
class Associations {
private var storage: [AnyHashable: Any] = [:]
func setValue(_ value: Any, forKey key: AnyHashable) {
import Foundation
import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true
func async<T>(_ executable: @escaping @autoclosure () -> T, _ priority: DispatchQoS.QoSClass = DispatchQoS.QoSClass.default, completion: @escaping (T) -> Void) {
DispatchQueue.global(qos: priority).async {
let result = executable()
DispatchQueue.main.async {
import Foundation
class CancellableDecorator {
private class Token {
var isValid = true
}
private var token: Token?
class Logger {
static var defaultLogger = Logger()
func log(_ message: String) {
print(message)
}
}
import UIKit
class TableViewContainerCell<Content: UIView>: UITableViewCell {
var content: Content
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
if let viewFromNib = Content.loadDefaultNib() {
content = viewFromNib
}
func performWithErrorPointer<T>(_ executable: @autoclosure @escaping () throws -> T, _ errorPointer: NSErrorPointer) -> T? {
do {
return try executable()
}
catch let error {
errorPointer?.pointee = error as NSError
return nil
}
}
import Foundation
import UIKit
class RTTableViewControllerSectionModel: NSObject {
let cellModels: [RTTableViewControllerCellModel]
let tableView: UITableView
required init(_ tableView: UITableView, _ cellModels: [RTTableViewControllerCellModel]) {
self.tableView = tableView