swift - 在枚举中使用泛型函数

标签 swift generics enums

我正在尝试使用枚举来包含通用函数。这些枚举将作为参数传递,然后可以相应地执行枚举中的函数。

如何在枚举定义中设置泛型类型,以便将它们识别为要执行的函数?请注意,我可能想要传入各种函数定义。

如果我在这里很荒谬,请告诉我。 :)


// Define an enum to pass into my APIs. The S and F are meant to be functions I can define in anyway
enum FormattedResult<S, F> {
    case Success(S)
    case Failure(F)

    func run<T> (a:T) {
        switch (self){
        case .Success (let completion):
            // QN: How do I execute this? completion() will of course fail
            debugPrint(completion)
        case .Failure (let failure):
            // QN: Same here
            debugPrint(failure)
        }
    }
}


// I want to define a callback for someone else to call. I will be passing this to the error
var k1 = FormattedResult<(Int)-> (), (String)->() >.Success(
    {(a: Int) in
        debugPrint("xxxxx")
    })

// the APIClient can run this upon completion
k1.run(2)

// similarly for failures
var k2 = FormattedResult<(Int)-> () , (String)->()>.Failure(
    {(error: String)  in
        debugPrint(error)
    }
    )
k2.run("some error happened...")

最佳答案

在原始代码中,虽然在创建变量k1k2时定义了一个闭包作为回调,但是SF 仍然只是占位符类型,并没有说明 SF 必须是什么。因此,这里的挑战是如何定义 Swift 枚举来存储给定函数类型的关联值。

所以我们的想法是我们可以使用诸如 (T) -> void 之类的函数类型作为枚举的参数类型,并将函数实现的某些方面与枚举案例值一起留给 when enum 函数被调用。

接下来我们不需要在枚举中使用两种占位符类型,因为每次调用函数 run(:) 时我们只有一种类型的 a即使它可能是 StringInt。这也是 Generic 的强大之处。虽然占位符类型 T 没有说明 T 必须是什么,但它确实说明了 a 和枚举的关联值 (T) -> void 必须是同一类型 T,无论 T 代表什么。因此,在这种情况下,一种类型的占位符就足够了。

实际上,我喜欢您的想法,即调用者可以传入一个值以由枚举中的 run 函数执行,并且您在原始代码中几乎做到了。以下是我上面提到的一个例子。

enum FormattedResult<T> {
    case Success(((T) -> Void))
    case Failure(((T) -> Void))

    func run(a:T) {
        switch self {
        case Success(let completion):
            completion(a)
        case Failure(let completion):
            completion(a)
        }
    }
}

let f1 = FormattedResult.Success({ a in
    debugPrint(a)
})
f1.run(1)

let f2 = FormattedResult.Failure({ error in
    debugPrint(error)
})
f2.run("some error happened...")

关于swift - 在枚举中使用泛型函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35761261/

相关文章:

swift - 如何将韩语单词拆分成它的组成部分?

java 。如何禁止使用原始泛型类型?

c# - 用于计算列表中给定子类出现次数的通用方法以及未找到 Type 参数的原因

java - JPA 枚举类型作为数据库中的表

ios - iOS 13 和 14 上的 NSKeyValuePopPendingNotificationLocal 崩溃

swift - Realm map 集群的自定义 View

ios - 我的 Xcode 项目坏了吗

java - 在 ListDataProvider 中使用通用列表

java - 枚举类型设置为 int 数组

java - 结合 Java 枚举和点语法 (enum.value1.value2)?