gpt4 book ai didi

json - Swift 4 中的配置错误

转载 作者:行者123 更新时间:2023-11-30 11:57:31 25 4
gpt4 key购买 nike

我收到以下错误:“‘配置’类型的值没有成员‘配置’”。基本上,我正在尝试建立与列出的 API 的连接。作为引用,我正在运行 Siesta 和 SwiftyJSON,这两个对我来说都是新的。任何帮助都会很棒。

import Foundation
import Siesta
import SwiftyJSON

let Pokeapi = _Pokeapi()

class _Pokeapi: Service {
public init() {
super.init(baseURL: "https://pokeapi.co/api/v2")
self.configure {
$0.config.pipeline[.parsing].add(SwiftyJSONTransformer, contentTypes: ["*/json"])
$0.config.expirationTime = 3600
}

}
}

public let SwiftyJSONTransformer =
ResponseContentTransformer
{ JSON($0.content as AnyObject) }

最佳答案

所以你得到的错误是因为闭包中的 $0 引用了 Configuration 类型的变量,该变量没有名为 config 的成员。相反,也许你想写:

self.configure { config in
config.pipeline[.parsing].add(SwiftyJSONTransformer, contentTypes: ["*/json"])
config.expirationTime = 3600
}

请注意,您也可以这样做

self.configure {
$0.pipeline[.parsing].add(SwiftyJSONTransformer, contentTypes: ["*/json"])
$0.expirationTime = 3600
}

但是如果你命名闭包变量,它会产生更好的可读代码

关于json - Swift 4 中的配置错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47640652/

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