Swift 通用序列可观察歧义
Swift generic sequence observable ambiguity
我有以下代码
protocol NamedOption {
var optionTitle: String { get }
}
struct DebugOption: NamedOption {
let optionTitle: String
let debugViewControllerType = UIViewController.self
}
func testFunk<T: Sequence>(d: Observable<T>) where T.Element == NamedOption {
}
func bindFullResultsRx() {
let dd: Observable<[DebugOption]> = self.dataModel.debugOptions // this is defined and properly assigned earlier
testFunk(d: dd)
}
在我调用 testFunk
Xcode 的那一行给我以下错误:
Expression type '()' is ambiguous without more context
我不知道为什么会这样 :( 到目前为止,我能够通过将 testFunk
上的约束更改为以下内容来使其工作:
func funk<T: NamedOption>(d: Observable<[T]>) {
}
在我看来,这比顶部的版本更具限制性。有谁知道如何让它与 T: Sequence
一起工作?
Xcode版本为9.4,Swift版本为4.1。
OO 和泛型不能很好地结合在一起。为了做你想做的事,你必须像这样手动投射:
testFunk(d: dd.map { [=10=].map { [=10=] as NamedOption } })
在工作同事的一些挖掘和帮助之后,我能够通过简单地将 ==
更改为 :
使其工作,所以它看起来像这样
func testFunk<T: Sequence>(d: Observable<T>) where T.Element: NamedOption {
}
这只是 swift 语法的问题
https://docs.swift.org/swift-book/ReferenceManual/GenericParametersAndArguments.html
conformance-requirement → type-identifier : protocol-composition-type
我有以下代码
protocol NamedOption {
var optionTitle: String { get }
}
struct DebugOption: NamedOption {
let optionTitle: String
let debugViewControllerType = UIViewController.self
}
func testFunk<T: Sequence>(d: Observable<T>) where T.Element == NamedOption {
}
func bindFullResultsRx() {
let dd: Observable<[DebugOption]> = self.dataModel.debugOptions // this is defined and properly assigned earlier
testFunk(d: dd)
}
在我调用 testFunk
Xcode 的那一行给我以下错误:
Expression type '()' is ambiguous without more context
我不知道为什么会这样 :( 到目前为止,我能够通过将 testFunk
上的约束更改为以下内容来使其工作:
func funk<T: NamedOption>(d: Observable<[T]>) {
}
在我看来,这比顶部的版本更具限制性。有谁知道如何让它与 T: Sequence
一起工作?
Xcode版本为9.4,Swift版本为4.1。
OO 和泛型不能很好地结合在一起。为了做你想做的事,你必须像这样手动投射:
testFunk(d: dd.map { [=10=].map { [=10=] as NamedOption } })
在工作同事的一些挖掘和帮助之后,我能够通过简单地将 ==
更改为 :
使其工作,所以它看起来像这样
func testFunk<T: Sequence>(d: Observable<T>) where T.Element: NamedOption {
}
这只是 swift 语法的问题 https://docs.swift.org/swift-book/ReferenceManual/GenericParametersAndArguments.html
conformance-requirement → type-identifier : protocol-composition-type