swift - 带有协议(protocol)对象的纯 swift 集

标签 swift set protocols

有没有办法让以下内容真正起作用?

V1——“测试协议(protocol)不符合Hashable”

protocol testProtocol  {
    //
}

class test {
    var s : Set<testProtocol>?
    init(){
    }
}

V2 - “协议(protocol) 'testProtocol' 只能用作通用约束,因为它具有 Self 或关联类型要求

protocol testProtocol : Hashable {
    //
}

class test {
    var s : Set<testProtocol>?
    init(){
    }
}

我假设答案是否定的——因为协议(protocol)(即使带有@objc 标签)没有足够的信息??但也许我在这里缺少某种线条或东西。

最佳答案

也许有更好的解决方案,但您可以使您的类通用:

protocol testProtocol : Hashable {
    //
}

class test<P: testProtocol> {
    var s : Set<P>?

    init() { }
}

关于swift - 带有协议(protocol)对象的纯 swift 集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29278624/

相关文章:

swift - 确定字符串是否包含 Swift 中集合中的字符的最佳方法是什么

batch-file - 在不同的行上询问 set/p 变量

时间:2019-03-08 标签:c#datetimepickersettime

swift - 带有 UISearchControllerDelegate 和/或 UISearchBarDelegate 和/或 UISearchController 的搜索栏

ios - swift 中 java 接口(interface)或 objective c 协议(protocol)的等价物是什么?

swift - 为什么我需要指定对象类型两次?

ios - 如何快速裁剪给定角度的图像

ios - swift : Background Color fading animation (SpriteKit)

swift - App Works 来自 Xcode,上传到 Apple 并作为 Developer Release 导出时崩溃

java - 将包含列表的实例映射到 flatMap(使用流)