Skip to content

Instantly share code, notes, and snippets.

@yutailang0119
Last active October 22, 2020 10:38
Show Gist options
  • Save yutailang0119/eab69e00c9b9f7ccefd6d139cc7be3e3 to your computer and use it in GitHub Desktop.
Save yutailang0119/eab69e00c9b9f7ccefd6d139cc7be3e3 to your computer and use it in GitHub Desktop.
AをBに変換するCombine.Publisher拡張を作りたい
import UIKit
import Combine
struct A {
let name: String
}
struct B {
let text: String
}
protocol Convertable {
associatedtype V
var converted: V { get }
// or
// associatedtype V
// func converted() -> V
// or
// func converted<V>() -> V
}
extension Publisher {
func converted() -> Publishers.Map<Self, Self.Output.V> where Self.Output == Convertable {
self.map { $0.converted }
}
}
extension A: Convertable {
var converted: B {
.init(text: self.name)
}
// or
// func converted() -> B {
// .init(text: self.name)
// }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment