Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Swift中,为什么CodingKey是我看到的与枚举一起使用的协议,用于在结构上使用可编码协议?_Swift_Xcode_Enums_Codable - Fatal编程技术网

在Swift中,为什么CodingKey是我看到的与枚举一起使用的协议,用于在结构上使用可编码协议?

在Swift中,为什么CodingKey是我看到的与枚举一起使用的协议,用于在结构上使用可编码协议?,swift,xcode,enums,codable,Swift,Xcode,Enums,Codable,在Swift中,为什么CodingKey是我看到的与枚举一起使用的协议,用于在结构上使用可编码协议 我没有用太多的方法来测试这个问题,但我总是在尝试复制我发现的所有示例时遇到这个错误。我通过在枚举上使用可编码协议获得了完美的行为 // This throws Error struct Foo: Codable { //! Type 'Foo' does not conform to protocol 'Codable' var id: String var name: Stri

在Swift中,为什么CodingKey是我看到的与枚举一起使用的协议,用于在结构上使用可编码协议

我没有用太多的方法来测试这个问题,但我总是在尝试复制我发现的所有示例时遇到这个错误。我通过在枚举上使用可编码协议获得了完美的行为

// This throws Error
struct Foo: Codable { //! Type 'Foo' does not conform to protocol 'Codable'

    var id: String
    var name: String
    var type: MyType
    var order: Int

    enum MyType: String, CodingKey {
        case this
        case that
        case and
        case theOtherThing
    }

}

// This doesn't
struct Foo: Codable {

    var id: String
    var name: String
    var type: MyType
    var order: Int

    enum MyType: String, Codable {
        case this
        case that
        case and
        case theOtherThing
    }
}

struct Foo的每个属性都必须是可编码的。这包括MyType。CodingKey指定将在JSON字典中使用的字符串,并不等同于Codable。id、name、order字段已可编码;由标准库提供

更新 将此扩展添加到Foo以更改将结构字段标签编码/解码为JSON的方式。我任意地在你的两处房产前加了我的钱

extension Foo {
  enum CodingKeys: String, CodingKey {
    case id
    case name
    case type = "my_type"
    case order = "my_order"
  }
}

struct Foo的每个属性都必须是可编码的。这包括MyType。CodingKey指定将在JSON字典中使用的字符串,并不等同于Codable。id、name、order字段已可编码;由标准库提供

更新 将此扩展添加到Foo以更改将结构字段标签编码/解码为JSON的方式。我任意地在你的两处房产前加了我的钱

extension Foo {
  enum CodingKeys: String, CodingKey {
    case id
    case name
    case type = "my_type"
    case order = "my_order"
  }
}
为什么要使用编码键

如果序列化数据格式中使用的键与 从数据类型中选择属性名称,通过 将字符串指定为 枚举

例如:-

Json-

创建符合可编码协议的结构

用法-

为什么要使用编码键

如果序列化数据格式中使用的键与 从数据类型中选择属性名称,通过 将字符串指定为 枚举

例如:-

Json-

创建符合可编码协议的结构

用法-


它如何指定将使用哪些字符串?你能给出一个快速的用例吗?谢谢它如何指定将使用哪些字符串?你能给出一个快速的用例吗?谢谢围绕这些东西的文档需要更好,或者我只需要开始更多地参与Swift社区的开发。围绕这些东西的文档需要更好,或者我只需要开始更多地参与Swift社区的开发。
struct UserData: Codable {
    var id: Int
    var name: String
    var email: String
    //Set JSON key values for fields in our custom type
    private enum CodingKeys: String, CodingKey {
        case id //Leave it blank because id matches with json Id
        case name = "name_Of_person"
        case email = "emailId"
    }
    init(from decoder: Decoder) throws {
        let values = try decoder.container(keyedBy: CodingKeys.self)
        id = try values.decode(Int.self, forKey: .id)
        name = try values.decode(String.self, forKey: .name)
        email = try values.decode(String.self, forKey: .email)
    }
}
 //Decode struct using JSONDecoder

    let jsonDecoder = JSONDecoder()
    do {
        let modelResult = try jsonDecoder.decode(UserData.self,from: jsonExample)
        print("id is \(modelResult.id) - Name is \(modelResult.name) - email is \((modelResult.email))")
    } catch {
        print(error)
    }