gpt4 book ai didi

swift - 仅当元素类型为特定类型时才扩展数组

转载 作者:搜寻专家 更新时间:2023-11-01 06:43:47 25 4
gpt4 key购买 nike

我想仅在其元素为特定类型时扩展数组类型。在这种情况下,我使用 EKCalendar 的实例。

这行不通:

public extension Array where Generator.Element : EKCalendar{

}

但是这样做:

public extension CollectionType where Generator.Element : EKCalendar{

}

为什么我只能使用协议(protocol)来做到这一点?

最佳答案

在扩展 Array 时,您无法访问 Generator.Element,因为它是 CollectionType 协议(protocol)的一部分,您需要使用 Element:

public extension Array where Element: EKCalendar {
//....
}

关于swift - 仅当元素类型为特定类型时才扩展数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32725446/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com