中〜大規模 Swift 開発における依存オブジェクト解決の方法を考える
- DI (Dependency Injection) とは?
- DIの基本的な話:知っている人が多かったら省略予定
- コンテナ (Swinject) を用いた DI
- Swinject の DI コンテナを用いた 動的DIの話
- 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 |
| public protocol A { | |
| func getA() -> String | |
| } | |
| private class AImpl: A { | |
| private func getA() -> String { | |
| return "a" | |
| } | |
| } |
RxSwiftライブラリの作り方をご紹介します。一つの記事ですべてを説明するのは非常に厳しいので、まず Observer や Observable といった基本的なコンポーネントとその周辺について、ひとつずつ作っていく流れで説明します。
対象コード
public enum Event<E> {
case Next(E)
case Error(ErrorType)
case Completed
}
public class Subject {| ; ModuleID = 'Hoge' | |
| target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128" | |
| target triple = "x86_64-apple-macosx10.9" | |
| %Vs5Int32 = type <{ i32 }> | |
| %Sp = type <{ i8* }> | |
| %swift.type = type { i64 } | |
| %swift.type_pattern = type opaque | |
| %swift.protocol_conformance = type { i32, i32, i32, i32 } | |
| %swift.protocol = type { i8*, i8*, i8*, i8*, i8*, i8*, i8*, i8*, i32, i32 } |
| enum Hoge { | |
| case Next | |
| case Error | |
| } | |
| func piyo(arg: Hoge?) -> Bool { | |
| return arg != nil | |
| } | |
| func foo(arg: Bool) -> Bool { |
| import Swift | |
| import Foundation | |
| let observable = BehaviorSubject(value: "cocoa") | |
| let observer = AnyObserver<String> { | |
| switch $0 { | |
| case .Next(let str): NSLog(str) | |
| default: break | |
| } | |
| } |
| ### nil check for boolean | |
| ``` | |
| % cat ./NilCheckForBoolean.swift | |
| import Foundation | |
| func target() { | |
| for _ in 1...100000 { | |
| let e: Bool? = true | |
| if e == nil { } |
| ; your code goes here | |
| (define (square x) (* x x)) | |
| (define (sum-of-square x y) (+ (square x) (square y))) | |
| (define (q13 x y z) (cond | |
| ((and (>= y x) (>= z x)) (sum-of-square y z)) | |
| ((and (>= x y) (>= z y)) (sum-of-square x z)) | |
| (else (sum-of-square x y)) | |
| )) |