0

How does the swift 4 Decodable protocol work with the ' - ' letter? For example:

[{
    "trigger": {
        "url-filter": "webkit.org",
        "resource-type": ["image"]
 },
    "action": {
        "selector": "#logo",
        "type": "block"
    }
}]

In My Swift Class:

struct blockerJson : Decodable {
    let action : action
    let trigger : trigger

    struct action : Decodable {
        let selector : String
        let type : String
    }

    struct trigger : Decodable {
        let urlFilter : String
        let resourceType : String
    }
}

I don't know how to change the class, but the json can't change...

Hamish
  • 78,605
  • 19
  • 187
  • 280
Kikkuru
  • 3
  • 1
  • 3
  • It works like any other supported character. – vadian Dec 26 '17 at 06:13
  • You need to do it by yourself as per your needs in a json response that you have. – Maulik Bhuptani Dec 26 '17 at 06:42
  • Possible duplicate of [How do I use custom keys with Swift 4's Decodable protocol?](https://stackoverflow.com/questions/44396500/how-do-i-use-custom-keys-with-swift-4s-decodable-protocol) – Hamish Dec 26 '17 at 09:51

1 Answers1

1

This code parses trigger block

struct Trigger: Decodable {
    var urlFilter: String
    var resourceType: [String]

enum CodingKeys: String, CodingKey {
        case urlFilter = "url-filter"
        case resourceType = "resource-type"
    }
}
Vyacheslav
  • 26,359
  • 19
  • 112
  • 194