我有以下代码
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。
最佳答案
在工作同事的一些挖掘和帮助之后,我能够通过简单地将 ==
更改为 :
使其工作,所以它看起来像这样
func testFunk<T: Sequence>(d: Observable<T>) where T.Element: NamedOption {
}
这只是一个快速语法的问题 https://docs.swift.org/swift-book/ReferenceManual/GenericParametersAndArguments.html
conformance-requirement → type-identifier : protocol-composition-type
关于Swift 通用序列可观察歧义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52674427/