ShawnMoore / XMLParsing

XMLEncoder & XMLDecoder using the codable protocol in Swift 4.2
MIT License
332 stars 100 forks source link

How to decode empty array? #21

Closed MihaelIsaev closed 6 years ago

MihaelIsaev commented 6 years ago

I have server response like this

<?xml version="1.0" encoding="UTF-8"?>
<add-ons type="array"/>

I'm trying to decode it as [AddOn]

public final class AddOn: BraintreeContent {
    public var amount: Double?
    public var currentBillingCycle: Int?
    public var description: String
}

but I'm getting error DecodingError.keyNotFound: Value required for key 'Index 0.description'. Is there any way to decode it properly into empty array or this framework just doesn't support decoding empty arrays?

ShawnMoore commented 6 years ago

You should try to decode it as an [AddOn]?. This will force the decoder to use decodeIfPresent that will return nil if the key does not exist.

MihaelIsaev commented 6 years ago

@ShawnMoore

You should try to decode it as an [AddOn]?. This will force the decoder to use decodeIfPresent that will return nil if the key does not exist.

I'm still getting DecodingError.keyNotFound: Value required for key 'Index 0.description'. even when I decoding it as [AddOn]?. Looks like it should be fixed