Swift 4 Decodable multiple containers
Tag : json , By : Robert Daniel Pickar
Date : March 29 2020, 07:55 AM
help you fix your problem Please read the JSON. Any opening { is quasi a separator. The indentation of the JSON indicates also the hierarchy. struct Root : Decodable {
let graphql : Graph
// to access the `Media` object declare a lazy instantiated property
lazy var media : Media = {
return graphql.shortcode_media
}()
}
struct Graph : Decodable {
let shortcode_media : Media
}
struct Media : Decodable {
let id: String
let shortcode : String
}
let jsonString = """
{
"graphql": {
"shortcode_media": {
"id": "1657677004214306744",
"shortcode": "BcBQHPchwe4"
}
}
}
"""
do {
let data = Data(jsonString.utf8)
var result = try decoder.decode(Root.self, from: data)
print(result.media)
} catch {
print("error: ", error)
}
|
Fatal error: Dictionary<String, Any> does not conform to Decodable because Any does not conform to Decodable
Tag : ios , By : ussballantyne
Date : March 29 2020, 07:55 AM
I hope this helps . Maybe you are misunderstanding how Codable works. It's based on concrete types. Any is not supported. In your case you might create a struct like struct Something: Decodable {
let success : Bool
let lastId : Int?
let hasMore: Bool
let foundEndpoint: URL
let error: String?
}
func loadLocalJSON() {
let url = Bundle.main.url(forResource: "localJSON", withExtension: "json")!
let data = try! Data(contentsOf: url)
let colors = try! JSONDecoder().decode(Something.self, from: data)
print(colors)
}
|
getting decodable error when trying to use decodable struct in another as array
Tag : swift , By : KingGuppy
Date : March 29 2020, 07:55 AM
I hope this helps . I'm getting in decodable error: does not conform to Decodable ... , It should be like this, struct Root2 : Decodable {
let services : [Services]
let nationalities : [Nationalities]
let packages : [Packages]
}
|
Swift Decodable fails for a class derived from a decodable compliant class (JSONDecoder is used for decoding)
Tag : json , By : Andrew Bailey
Date : March 29 2020, 07:55 AM
will be helpful for those in need You need to implement init(from decoder: Decoder) in the subclass and call the superclass init although you don't need to implement it for the superclass init for CardListResponse, notice the call to super.init required public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
cards = try container.decode([DebitCard].self, forKey: .cards)
activeCardId = try container.decode(Int.self, forKey: .activeCardId)
try super.init(from: decoder)
}
public class CardListResponse: Decodable {
public var cards: [DebitCard]?
public var activeCardId: Int?
public var details: [CodeAndDetails]
private enum CodingKeys: String, CodingKey {
case cards = "row"
case activeCardId = "CurrentActiveId"
case details = "Success"
}
}
|
Array of Decodable type is not decodable
Date : March 29 2020, 07:55 AM
Hope this helps If we have a type Foo: Decodable how can we make Array decodable? , Just use this: let res = JSONDecoder().decode([Foo].self, data)
|