Skip to content

Instantly share code, notes, and snippets.

View tarunon's full-sized avatar
🐌

tarunon tarunon

🐌
View GitHub Profile
protocol A {
associatedtype X
func hoge(_ arg: X)
}
extension A where X == Void {
func hoge(_ arg: Void) {
}
}
@tarunon
tarunon / genericsviewmodel.swift
Last active January 6, 2017 08:13
Generics ViewModel
//: Playground - noun: a place where people can play
import UIKit
protocol ViewModelType {
associatedtype ViewType
associatedtype ModelType
var base: ModelType { get }
var fields: [String: Any] { get set }
@tarunon
tarunon / transitioning.swift
Created December 15, 2016 16:31
Type-safe ViewControllerTransition wrapper
import Foundation
enum TransitioningMode {
case present
case dismiss
}
struct TransitioningContext<Presenting: UIViewController, Presented: UIViewController> {
let native: UIViewControllerContextTransitioning
let mode: TransitioningMode
@tarunon
tarunon / TypedErrorOnRx.swift
Created December 5, 2016 14:49
It's small idea.
protocol TypedError: Error {
init(error: Error)
}
struct AnyError: TypedError {
let error: Error
init(error: Error) {
self.error = error
}
}
@tarunon
tarunon / Extensions.swift
Last active November 22, 2016 10:12
Himotoki+Barrel
//
// Extensions.swift
// Barrel_Himotoki
//
// Created by Nobuo Saito on 2016/11/22.
// Copyright © 2016年 line. All rights reserved.
//
import Foundation
import Himotoki
struct AnyJSON {
enum Error: Swift.Error {
case failToTransform
}
let rawValue: Any
static func transformer() -> Transformer<AnyJSON, String> {
return Transformer { (anyJSON) throws -> String in
switch anyJSON.rawValue {
@tarunon
tarunon / invoke.swift
Last active May 7, 2016 13:26
dynamic invocation in swift
func wrap<A, B, C>(f: A -> B -> C) -> (A -> Any -> Any) {
return { a1 in { a2 in f(a1)(a2 as! B) } }
}
protocol Dynamic {
static var functions: [Selector: Self -> Any -> Any] { get }
func dynamicInvoke(f: Selector, arg: Any) -> Any
}
extension Dynamic {
@tarunon
tarunon / Router.swift
Created March 4, 2016 01:43
App URL Schemeをマッチングしてみる
import Foundation
enum URLMatchResult<T> {
case Match(T)
case Mismatch
}
protocol URLHandlerBaseType {
func handle(url: NSURL)
}
@tarunon
tarunon / typedWildcardError.swift
Last active January 15, 2016 09:35
workaround Wildcard error type (is typed)
enum Error1<E1: ErrorType> {
case ERROR1(E1)
init?(error: ErrorType) {
if let error = error as? E1 {
self = .ERROR1(error)
}
return nil
}
@tarunon
tarunon / error.swift
Created January 15, 2016 08:58
workaround wildcard ErrorType generics.
import UIKit
enum Error: ErrorType {
case ERROR(ErrorType)
}
var error: ErrorType = Error.ERROR(NSCocoaError.CoderReadCorruptError)
switch error {
case Error.ERROR(NSCocoaError.CoderReadCorruptError):