codable

    5热度

    2回答

    我试图执行新的Codable协议,所以我在我的结构中添加了Codable,但我的卡在解码JSON上。 这是我收到: 结构 - struct Question { var title: String var answer: Int var question: Int } 客户端 - ... guard let data = data else { ret

    0热度

    1回答

    好吧,用Swift 4编写一些网络代码并用它们新的可解码协议来修补。下面的错误不与几个编译器错误: // A lot has been stripped out of this for brevity, so we can focus on this specific constant struct APIRequest { let decoder = JSONDecoder()

    23热度

    4回答

    应该使用类继承来破坏类的可解析性。例如,下面的代码 class Server : Codable { var id : Int? } class Development : Server { var name : String? var userId : Int? } var json = "{\"id\" : 1,\"name\" : \"Large B

    2热度

    2回答

    实施例: import Foundation class Player: Codable { let name: String init(name: String) { self.name = name } } class Team: Codable { let players: [Player] let capitan: P

    21热度

    1回答

    斯威夫特4添加了新的Codeable协议。当我使用JSONDecoder时,它似乎要求我的Codeable类的所有非可选属性在JSON中都有键,否则会引发错误。 使我的类的每个属性可选看起来像一个不必要的麻烦,因为我真正想要的是使用json中的值或默认值。 (我不希望财产为零)。 有没有办法做到这一点? class MyCodable: Codable { var name: Strin

    2热度

    1回答

    注意:我已经看过了这个问题 - >How do I use custom keys with Swift 4's Decodable protocol?但它并没有解释如何编码/解码枚举 这里是我想要的结构: struct MyStruct: Decodable { let count: PostType } enum PostType: Decodable { case

    1热度

    1回答

    如何使用Codable解码JSON并在创建时交叉引用对象(不是结构体)?在这个例子中,我希望Painting类具有也在JSON中定义的Color对象的数组。 (我也希望能够将它们编码回JSON了。) 奖励:在这种情况下,我宁愿Painting.colors是一个非可选let属性而不是var。我不希望它在创建后改变,我不希望它永远是零。 (我宁愿使用一个空数组,而不是零的默认值。) class Ar

    8热度

    4回答

    Swift的JSONDecoder提供dateDecodingStrategy属性,它允许我们根据DateFormatter对象定义如何解释传入日期字符串。 但是,我目前正在使用一个API,该API返回日期字符串(yyyy-MM-dd)和日期时间字符串(yyyy-MM-dd HH:mm:ss),具体取决于属性。有没有办法让JSONDecoder处理这个问题,因为提供的DateFormatter对象

    15热度

    4回答

    我正在使用带有JSON数据的Swift 4 Codable协议。我的数据被格式化使得存在在根级别的单个密钥与含有我需要的属性,诸如对象值: { "user": { "id": 1, "username": "jdoe" } } 我有一个User结构,可以解码该user键: struct User: Codable { let id: Int

    3热度

    2回答

    这是我的代码。但我不知道该如何设定价值。它必须手动完成,因为实际结构比这个例子稍微复杂一些。 请帮忙吗? struct Something: Decodable { value: [Int] enum CodingKeys: String, CodingKeys { case value } init (from decoder :Decod