Skip to content

Instantly share code, notes, and snippets.

@ukitaka
ukitaka / command
Created February 12, 2016 05:48
NSDate(timeIntervalSince1970: 0) == 0 がコンパイルエラーにならない件
$ swiftc -emit-silgen -sdk $(xcrun --show-sdk-path --sdk macosx) date.swift > date.sil
struct PartialFunction<A, B> {
let f: A throws -> B
init(_ f: A throws -> B) {
self.f = f
}
}
extension Optional {
func collect<B>(f: PartialFunction<Wrapped, B>) -> Optional<B> {
if let res = try? self.map(f.f) {
@ukitaka
ukitaka / Repository.swift
Created May 30, 2016 02:14
Repository impl helper(with Realm, Future)
@_exported import class BrightFutures.Future
import func BrightFutures.future
import var BrightFutures.ImmediateOnMainExecutionContext
import RealmSwift
protocol RepositoryImplType {
func runWrite(f: (Realm) throws -> Void) -> Future<Void, RepositoryError>
func runRead<T>(f: (Realm) throws -> T) -> Future<T, RepositoryError>
func runReadAndBlock<T>(f: (Realm) throws -> T) throws -> T
func runWriteAndBlock(f: (Realm) throws -> Void) throws -> Void
@ukitaka
ukitaka / RealmInstanceManager.swift
Created May 30, 2016 02:18
Realmのインスタンス生成
import RealmSwift
private let RealmSchemaVersion: UInt64 = 1
private let DefaultInMemoryRealmIndentifier = "..............."
private func realmfileURL(fileName: String, bundleIdentifier: String) -> NSURL? {
#if os(tvOS)
let path: NSString? = NSSearchPathForDirectoriesInDomains(.CachesDirectory, .UserDomainMask, true).first
#elseif os(iOS)
let path: NSString? = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).first
@ukitaka
ukitaka / DI.swift
Last active July 22, 2016 06:42
SwinjectとProtocol Extensionを用いたDIパターン
@_exported import class Swinject.Container
public struct DIContainer {
public static let container = Container()
private init() { }
public static func regist(@noescape block: Container -> Void) {
container.removeAll()
block(container)
@ukitaka
ukitaka / Entity.swift
Last active June 27, 2016 12:02
SwiftでDDD - Entity用のprotocol
public protocol Entity: Equatable {
associatedtype ID: Equatable
var id: ID { get }
}
public func ==<E: Entity>(lhs: E, rhs: E) -> Bool {
return lhs.id == rhs.id
}
@ukitaka
ukitaka / CleanseSample.swift
Created June 28, 2016 06:02
Lightweight DI Framework Cleanse memo
// 1. インターフェースをつくる
protocol UserRepository {
func findByID(userID: UserID) -> User
}
// 2. 実装を作る
class UserDAO: UserRepository {
init() { ... }
@ukitaka
ukitaka / const.swift
Last active July 2, 2016 09:46
Haskell.swift
func const<A, B>(a: A) -> B -> A {
return { _ in a }
}
protocol OptionalType {
associatedtype Wrapped
func map<U>(@noescape f: (Wrapped) throws -> U) rethrows -> U?
}
extension Optional: OptionalType {
}
extension ObservableType where E: OptionalType {
@ukitaka
ukitaka / CaptureSelf.swift
Last active September 3, 2016 15:50
クロージャー受け取るところにメソッド渡すと、メソッドが生えてるオブジェクト強参照する..
textField.rx_text
.map(myMethod) // selfが強参照される → リークする
.subscribeNext { _ in }
.addDisposableTo(disposeBag)
textField.rx_text
.map { [unowned self] in self.myMethod($0) } // 長いけどこう書くしかない
.subscribeNext { _ in }
.addDisposableTo(disposeBag)