gpt4 book ai didi

带有闭包属性的 Swift 4.0 解码器错误

转载 作者:搜寻专家 更新时间:2023-11-01 05:33:58 24 4
gpt4 key购买 nike

我正在尝试使用 Swift 4.0 Codable 协议(protocol)序列化我的对象。我在尝试解码闭包属性时遇到错误:

guard let influenceFunction = try? container.decode(((E, Double) -> (E))!.self, forKey: TransformCodingKeys.influenceFunction) else {
// ... do something clever ...
print("sad times...")
}

Cannot invoke 'decode' with an argument list of type '(((E, Double) -> (E))!.Type, forKey: TransformCodingKeys)'

我想这很容易理解,但肯定有一些我可以使用的策略(毕竟,函数是一流的对象,对吧?)。我必须以某种方式包装我的闭包吗?

最佳答案

你可以使用一个技巧来解决这个问题:

typealias YourCompletion = (_ status: Bool) -> Void
class Completion: Codable {
var completion: YourCompletion?
private enum CodingKeys: CodingKey {}
}

关于带有闭包属性的 Swift 4.0 解码器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45970903/

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