What does quot;Protocol ... can only be used as a generic constraint because it has Self or associated type requirementsquot; mean?(“Protocol ... can only be used as a generic constraint because it has Self or associated type requirements是什么意思?意思是?)
问题描述
我正在尝试创建一个字典(实际上是一个 HashSet)键入自定义协议斯威夫特,但它给了我标题中的错误:
I am trying to create a Dictionary (actually a HashSet) keyed on a custom protocol in Swift, but it is giving me the error in the title:
Protocol 'myProtocol' 只能用作通用约束,因为它具有 Self 或关联的类型要求
Protocol 'myProtocol' can only be used as a generic constraint because it has Self or associated type requirements
而且我无法判断它的正面或反面.
and I can't make heads nor tails of it.
protocol Observing: Hashable { }
var observers = HashSet<Observing>()
推荐答案
协议 Observing
继承自协议 Hashable
,而协议又继承自协议 Equatable代码>.协议
Equatable
有如下要求:
Protocol Observing
inherits from protocol Hashable
, which in turn inherits from protocol Equatable
. Protocol Equatable
has the following requirement:
func ==(lhs: Self, rhs: Self) -> Bool
并且在其内部某处包含 Self
的协议不能在任何地方使用,除非在类型约束中.
And a protocol that contains Self
somewhere inside it cannot be used anywhere except in a type constraint.
这里是一个类似的问题.
这篇关于“Protocol ... can only be used as a generic constraint because it has Self or associated type requirements"是什么意思?意思是?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:“Protocol ... can only be used as a generic constraint because it has Self or associated type requirements"是什么意思?意思是?


基础教程推荐
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01