Skip to content

Instantly share code, notes, and snippets.

View Otbivnoe's full-sized avatar

Nikita Ermolenko Otbivnoe

View GitHub Profile
extension RawRepresentable {
init?(value: Self.RawValue?) {
if let value = value {
self.init(rawValue: value)
}
else {
return nil
}
}
}
@Otbivnoe
Otbivnoe / ObjectMapper-Coding.swift
Created December 22, 2016 09:19
Convenient object coding via ObjectMapper - Example
class Base: NSObject, NSCoding, StaticMappable {
var d: Int?
override init() {}
init(d: Int?) {
self.d = d
}
extension ObservableType {
func flatMap<A: AnyObject, O: ObservableType>(weak obj: A, selector: @escaping (A, Self.E) throws -> O) -> Observable<O.E> {
return flatMap { [weak obj] value -> Observable<O.E> in
try obj.map { try selector($0, value).asObservable() } ?? .empty()
}
}
func flatMapFirst<A: AnyObject, O: ObservableType>(weak obj: A, selector: @escaping (A, Self.E) throws -> O) -> Observable<O.E> {
return flatMapFirst { [weak obj] value -> Observable<O.E> in
login(username: "Username", password: "Password")
.flatMap { [weak self] user -> Observable<[Post]> in
return self.loadPosts(for: user)
}
.subscribe()
login(username: "Username", password: "Password")
.flatMap { [weak self] user -> Observable<[Post]> in
guard let `self` = self else {
return Observable.empty()
}
return self.loadPosts(for: user)
}
.subscribe()
extension ObservableType {
func flatMap<A: AnyObject, O: ObservableType>(weak obj: A, selector: @escaping (A, Self.E) throws -> O) -> Observable<O.E> {
return flatMap { [weak obj] value -> Observable<O.E> in
try obj.map { try selector($0, value).asObservable() } ?? .empty()
}
}
}
login(username: "Username", password: "Password")
.flatMap(weak: self) { obj, user -> Observable<[Post]> in
return obj.loadPosts(for: user)
}
.subscribe()
osx_image: xcode8.3
xcode_workspace: HelloOpenSource.xcworkspace
matrix:
include:
- os: linux
dist: trusty
sudo: required
language: generic
- xcode_scheme: HelloOpenSource macOS
@Otbivnoe
Otbivnoe / ServicesComposition.swift
Created April 13, 2017 02:55 — forked from artemnovichkov/ServicesComposition.swift
Using protocol composition and generics for dependency injection in Interactors (VIPER, SOA, another cool keywords...)
protocol HasService1 {
var service1: Service1 { get }
}
protocol HasService2 {
var service2: Service2 { get }
}
class Service1 {}
class Service2 {}
static public func create() -> Disposable {
return NopDisposable.noOp
}