対象コード
public enum Event<E> {
case Next(E)
case Error(ErrorType)
case Completed
}
public class Subject {対象コード
public enum Event<E> {
case Next(E)
case Error(ErrorType)
case Completed
}
public class Subject { RxSwiftライブラリの作り方をご紹介します。一つの記事ですべてを説明するのは非常に厳しいので、まず Observer や Observable といった基本的なコンポーネントとその周辺について、ひとつずつ作っていく流れで説明します。
| public protocol A { | |
| func getA() -> String | |
| } | |
| private class AImpl: A { | |
| private func getA() -> String { | |
| return "a" | |
| } | |
| } |
| /// 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 |
| 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> |