Swift中的泛型和协议:为什么我的$0没有期望成员?

Swift中的泛型和协议:为什么我的$0没有期望成员?,swift,Swift,在下面的代码中,当我尝试let expectations=stuff-toexpect.map({$0.expectation})时,编译器会说元组类型的值“(key:_,Value:haspeections)”没有成员“expectation” 将map in与泛型类型一起使用的正确方法是什么 import XCTest import Foundation protocol HasExpectations { var expectation: X

在下面的代码中,当我尝试
let expectations=stuff-toexpect.map({$0.expectation})
时,编译器会说元组类型的值“(key:_,Value:haspeections)”没有成员“expectation”

将map in与泛型类型一起使用的正确方法是什么

    import XCTest
    import Foundation    
    protocol HasExpectations {
        var expectation: XCTestExpectation { get }
    }

    public class A: HasExpectations {
        var expectation: XCTestExpectation

        init(expectation: XCTestExpectation) {
            self.expectation = expectation
        }
    }

    public class B: HasExpectations {
        var expectation: XCTestExpectation

        init(expectation: XCTestExpectation) {
            self.expectation = expectation
        }
    }

    func doit<T>(stuffToExpect: [T: HasExpectations]) {
        let expectations = stuffToExpect.map({ $0.expectation })
    }
</pre>
导入XCTest
进口基金会
协议有预期{
变量期望值:xctestexpection{get}
}
公开甲级:哈斯期望{
var预期:xTestExpection
init(期望值:xctestexpection){
自我期望=期望
}
}
B级公共课:高期望{
var预期:xTestExpection
init(期望值:xctestexpection){
自我期望=期望
}
}
func doit(stuffToExpect:[T:HasExpections]){

让expections=stuffToExpect.map({$0.expectation}) }
在您的函数中

func doit<T>(stuffToExpect: [T: HasExpectations]) {
    let expectations = stuffToExpect.map({ $0.expectation })
}
在你的职责范围内

func doit<T>(stuffToExpect: [T: HasExpectations]) {
    let expectations = stuffToExpect.map({ $0.expectation })
}
调用超出预期的映射值

func doit<T>(stuffToExpect: [T: HasExpectations]) {
    let expectations = stuffToExpect.mapValues { $0.expectation }
    // here your expectations is a new dictionary of [Hashable: XCTestExpectation]
    // also note that T is turned to Hashable since you cannot have stuffToExpect key with T, which is not actually Hashable.

    // you can get all test expectations using values
    let testExpectations = Array(expectations.values)
    // here testExpectations is [XCTestExpectation]
}
func doit(stuffToExpect:[T:hasExpections]){

让Expections=stuffToExpect.mapValues{$0.expectation} //这里您的期望是[Hashable:XTestExpection]的新字典 //还要注意的是,T变成了可散列的,因为不能将stuffToExpect键与T一起使用,因为T实际上是不可散列的。 //您可以使用值获得所有测试期望值 让TestExpections=Array(expectations.values) //这里的TestExpections是[XTestExpection] }
调用超出预期的映射值

func doit<T>(stuffToExpect: [T: HasExpectations]) {
    let expectations = stuffToExpect.mapValues { $0.expectation }
    // here your expectations is a new dictionary of [Hashable: XCTestExpectation]
    // also note that T is turned to Hashable since you cannot have stuffToExpect key with T, which is not actually Hashable.

    // you can get all test expectations using values
    let testExpectations = Array(expectations.values)
    // here testExpectations is [XCTestExpectation]
}
func doit(stuffToExpect:[T:hasExpections]){

让Expections=stuffToExpect.mapValues{$0.expectation} //这里您的期望是[Hashable:XTestExpection]的新字典 //还要注意的是,T变成了可散列的,因为不能将stuffToExpect键与T一起使用,因为T实际上是不可散列的。 //您可以使用值获得所有测试期望值 让TestExpections=Array(expectations.values) //这里的TestExpections是[XTestExpection] }
您将
T
描述为通用。这是正确的。现在,您想说我很高兴接受符合协议的任何类型
T
。这意味着

你的函数如下所示

func doit<T: HasExpectations>(stuffToExpect: [T]) {
    let expectations = stuffToExpect.map({ $0.expectation })
}
func doit(stuffToExpect:[T]){

让expections=stuffToExpect.map({$0.expectation}) }

由于指定
[T:haseExpections]
时出现编译错误。编译器将其视为字典,但这里不是这样。

您将
T
描述为泛型。这是正确的。现在,您想说我很高兴接受符合协议的任何类型
T
。这意味着

你的函数如下所示

func doit<T: HasExpectations>(stuffToExpect: [T]) {
    let expectations = stuffToExpect.map({ $0.expectation })
}
func doit(stuffToExpect:[T]){

让expections=stuffToExpect.map({$0.expectation}) }

由于指定
[T:haseExpections]
时出现编译错误。编译器将其视为字典,但这里的情况并非如此。

我真正的问题是我的函数声明:

func doit(stuffToExpect:[T:hasExpections]){

我真正想做的是说T符合他的期望:


func-doit(Stuff-ToExpect:[T:hasExpections]){
我真正的问题是我的函数声明:

func doit(stuffToExpect:[T:hasExpections]){

我真正想做的是说T符合他的期望:


func-doit(Stuff-ToExpect:[T:HasExpections]){
let-expections=Stuff-ToExpect.map{$0.expection}->
T类型的值没有成员“expectation”
let-expections=Stuff-ToExpect.map{$0.expectation}->
类型“T”的值没有成员“期望值”
虽然这篇文章不是答案,但它确实帮助我找到了问题所在(我通过了一本T字典:值符合HasExpections,而不仅仅是类型T符合HasExpections)虽然这篇文章不是答案,但它确实帮助我指出了我的问题所在(我传递的是一本T字典:其值符合HasExpections,而不仅仅是T类型符合HasExpections)。