我现在正在运行 Codables。但 API 有一些字符串条目,如果它们为空,有时可能会出现Int值。0我在这里搜索并发现了这个:Swift 4 Codable - Bool or String Values但我无法让它运行
我的结构
struct check : Codable {
let test : Int
let rating : String?
}
Run Code Online (Sandbox Code Playgroud)
评级大多数时候是这样的"1Star"。但如果没有评级,我会0以 Int 的身份返回。
我正在这样解析数据:
enum Result<Value> {
case success(Value)
case failure(Error)
}
func checkStar(for userId: Int, completion: ((Result<check>) -> Void)?) {
var urlComponents = URLComponents()
urlComponents.scheme = "https"
urlComponents.host = "xyz.com"
urlComponents.path = "/api/stars"
let userIdItem = URLQueryItem(name: "userId", value: "\(userId)")
urlComponents.queryItems = [userIdItem] …Run Code Online (Sandbox Code Playgroud) 我使用API来解析一些数据 - 这很有效.但是现在api使用了像"d/a"和"test%"这样的名字,我无法用这个符号创建一个可编码的结构.有没有办法使用它们?
提前致谢