Skip to content

Instantly share code, notes, and snippets.

View Otbivnoe's full-sized avatar

Nikita Ermolenko Otbivnoe

View GitHub Profile
osx_image: xcode8.3
xcode_workspace: HelloOpenSource.xcworkspace
matrix:
include:
- os: linux
dist: trusty
sudo: required
language: generic
- xcode_scheme: HelloOpenSource macOS
login(username: "Username", password: "Password")
.flatMap(weak: self) { obj, user -> Observable<[Post]> in
return obj.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] user -> Observable<[Post]> in
guard let `self` = self else {
return Observable.empty()
}
return self.loadPosts(for: user)
}
.subscribe()
login(username: "Username", password: "Password")
.flatMap { [weak self] user -> Observable<[Post]> in
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()
}
}
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
@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 RawRepresentable {
init?(value: Self.RawValue?) {
if let value = value {
self.init(rawValue: value)
}
else {
return nil
}
}
}