小编Nxt*_*t97的帖子

Swift 4 Codable - API 有时提供 Int,有时提供 String

我现在正在运行 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 swift swift4 codable

8
推荐指数
1
解决办法
2361
查看次数

在Codable - Swift中使用"/"和"%"

我使用API​​来解析一些数据 - 这很有效.但是现在api使用了像"d/a"和"test%"这样的名字,我无法用这个符号创建一个可编码的结构.有没有办法使用它们?

提前致谢

api json struct swift codable

1
推荐指数
1
解决办法
68
查看次数

标签 统计

api ×2

codable ×2

swift ×2

json ×1

struct ×1

swift4 ×1