【问题标题】:can swift functions and closures conform to Hashable?swift 函数和闭包可以符合 Hashable 吗?
【发布时间】:2016-02-02 19:08:01
【问题描述】:

假设我想要一个Set 的函数或闭包。以下是我的处理方式:

typealias HandlerX = () -> ()
static var handlersX = Set<HandlerX>()

这会产生以下编译器错误:

类型'HandlerX'(又名'() -> ()')不符合协议'Hashable'

这是一个死胡同吗?

【问题讨论】:

  • 函数数组可以解决您的问题吗?如果是,你可以有一个这样的数组 => var funcArray = Array Any> = [func1, func2]

标签: ios swift macos function closures


【解决方案1】:

是的,这是一条死胡同。 Hashable 并不是你的问题。没有办法决定两个闭包是否相等(这是 Hashable 的基本要求)。

【讨论】:

    猜你喜欢
    • 2021-02-04
    • 2020-09-02
    • 2017-07-11
    • 2018-10-09
    • 2022-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多