Skip to content

Instantly share code, notes, and snippets.

View 53ningen's full-sized avatar
🐰
Is the Order a Rabbit?

gomi_ningen 53ningen

🐰
Is the Order a Rabbit?
View GitHub Profile

対象コード

public enum Event<E> {
    case Next(E)
    case Error(ErrorType)
    case Completed
}

public class Subject {

RxSwiftライブラリの作り方 Observer/Observable編

 RxSwiftライブラリの作り方をご紹介します。一つの記事ですべてを説明するのは非常に厳しいので、まず ObserverObservable といった基本的なコンポーネントとその周辺について、ひとつずつ作っていく流れで説明します。

注意事項

  • この記事の内容を理解しなくても RxSwift は十分使えるライブラリです
  • まだ Rx 系のライブラリを使ったことがない方は、まずライブラリを使ってみてください
  • ただ、記事の内容的には Rx 系ライブラリの利用経験がなくても分かるように書いたつもりです
  • 以下の実装は RxSwift のものであり、他言語の Rx ライブラリとは実装が異なる場合があります
@53ningen
53ningen / play_with_protocol.swift
Last active March 15, 2016 16:06
Playgroundで動きます (Xcode7.2)
public protocol A {
func getA() -> String
}
private class AImpl: A {
private func getA() -> String {
return "a"
}
}

Dependency Injection in Swift 2

今日話す内容

中〜大規模 Swift 開発における依存オブジェクト解決の方法を考える

  1. DI (Dependency Injection) とは?
  • DIの基本的な話:知っている人が多かったら省略予定
  1. コンテナ (Swinject) を用いた DI
  • Swinject の DI コンテナを用いた 動的DIの話
  1. Swiftにおける静的 DI
/// DEFINE protocol A, class AImpl
protocol A { func getA() -> String }
private class AImpl: A{ func getA() -> String { return "a" } }
protocol AComponent {
static var a: A { get }
static func createA() -> A
}
/// DEFINE AComponent, DefaultAComponent
typealias ContextType = protocol<AComponent, ABComponent>
class Context {
private let type: ContextType.Type
init(type: ContextType.Type) { self.type = type }
var a: A { return type.a }
var ab: AB { return type.ab }
}
class DebugContext: DefaultAComponent, DefaultABComponent {
static let a: A = DebugContext.createA()
static let ab: AB = DebugContext.createAB()
protocol C { func appendC(s: String) -> String }
class CImpl: C {
private let b: B
init(b: B) { self.b = b } //⚡️ CImpl は B のインスタンスを要求する
func appendC(s: String) -> String { return b.appendB(s) + "c" }
}
protocol B { func appendB(s: String) -> String }
class BImpl: B {
private let a: A
@53ningen
53ningen / di.swift
Last active February 8, 2016 19:15
public protocol Service {
func addPrefix(name: String) -> String
}
public class MsService: Service {
public func addPrefix(name: String) -> String { return "Ms." + name }
}
public class MrService: Service {
public func addPrefix(name: String) -> String { return "Mr." + name }
button.rx_tap
.flatMap { () -> Observable<String> in
if (self.textField3.text?.characters.count > 0) ?? false {
return just(String(self.textField3.text!))
} else {
return failWith(NSError(domain: "app error", code: -1, userInfo: nil))
}
}
.subscribe(
onNext: { self.showAlert("値は\($0)") },
interface ImmediateSchedulerType
interface SchedulerType
ImmediateSchedulerType <|- SchedulerType
SchedulerType <|- SerialDispatchQueueScheduler
SerialDispatchQueueScheduler <|- MainScheduler
interface ImmediateSchedulerType {
+ schedule<StateType>