gpt4 book ai didi

Set 类型的 Swift 协议(protocol)属性

转载 作者:行者123 更新时间:2023-11-28 12:21:37 25 4
gpt4 key购买 nike

我正在尝试创建简单的协议(protocol):

protocol Groupable
{
var parent: Self? { get }
var children: Set<Self> { get }
}

但是 children 属性无法编译,因为:Type 'Self' 不符合协议(protocol) 'Hashable'

有什么方法可以确保 SelfHashable 吗?或者任何其他解决方法,例如使用 associatedtype

最佳答案

在协议(protocol)中放置约束的方式类似于指定协议(protocol)一致性的方式(毕竟它们是同一回事)

protocol Groupable: Hashable
{
var parent: Self? { get }
var children: Set<Self> { get }
}

关于Set<Self> 类型的 Swift 协议(protocol)属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44629030/

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