Skip to content

Instantly share code, notes, and snippets.

import CasePaths
public protocol CaseProviding {}
extension CaseProviding {
public subscript<Value>(casePath casePath: CasePath<Self, Value>) -> Value? {
get { casePath.extract(from: self) }
set {
guard let value = newValue else { return }
self = casePath.embed(value)
extension Range where Bound: Int {
func random() -> Int {
return min + Int(arc4random_uniform(UInt32(upperBound - lowerBound)))
}
}
import UIKit
class SingleCellTableView<Cell>: UITableView {
override init(frame: CGRect, style: UITableViewStyle) {
super.init(frame: frame, style: style)
registerCell()
}
func registerCell() {
guard let cellClass = Cell.self as? AnyClass else {
extension ImagePreheatingController {
public var rx_delegate: DelegateProxy {
return proxyForObject(RxImagePreheatingControllerDelegateProxy.self, self)
}
public var rx_preheatItems: Observable<([Int], [Int])> {
return rx_delegate.observe("preheatingController:didUpdateWithAddedIndexPaths:removedIndexPaths:")
.map { a in
print(a)
return ([], [])
import Foundation
class NotificationManager
{
static let sharedManager = NotificationManager()
var observersMap = [String:NSHashTable]()
func add<T>(type: T.Type, observer: T) {
let typeString = "\(T.self)"
let observers: NSHashTable
@matthewcheok
matthewcheok / Promote.swift
Created August 7, 2015 23:29
Type promotion
// fill in the blanks with mismatched lhs and rhs types
func +(lhs: Int, rhs: Double) -> Double {
return Double(lhs) + rhs
}
func +(lhs: Double, rhs: Int) -> Double {
return lhs + Double(rhs)
}
func +(lhs: Int, rhs: Float) -> Float {
@matthewcheok
matthewcheok / node.swift
Created July 31, 2015 04:39
Linked List
class NodeImpl {
var node: Node?
func copy() -> NodeImpl {
let copy = NodeImpl()
copy.node = node
return copy
}
}
@matthewcheok
matthewcheok / Parameters.swift
Created July 24, 2015 11:00
Swift parameter lists as tuples
func foo(a: Int, what b: Int, _ name: String) -> Void {
print(a)
print(b)
print(name)
}
let arguments = (4, what: 3, "hello")
foo(arguments)
@matthewcheok
matthewcheok / gist:b673c6da717e97ec95db
Last active August 29, 2015 14:24
NSURL with Operators
import Foundation
func +(lhs: NSURL, rhs:String) -> NSURL {
return lhs.URLByAppendingPathComponent(rhs)
}
let url = NSURL(string: "http://google.com")!
url
url + "pages"
@matthewcheok
matthewcheok / Deferrable.swift
Last active August 29, 2015 14:24
Defer for Swift 1.2
class Deferrable {
let closure: ()->Void
init(closure: ()->Void) {
self.closure = closure
}
deinit {
closure()
}