nalexn / clean-architecture-swiftui

SwiftUI sample app using Clean Architecture. Examples of working with CoreData persistence, networking, dependency injection, unit testing, and more.
MIT License
5.57k stars 671 forks source link

Decoding JSON to Swift Model Not Root Level #17

Closed ahmadabudawod closed 4 years ago

ahmadabudawod commented 4 years ago

I have one case related to decoding JSON, while I have created a model based on the JSON structure which is dictionary but the data that I need is in nested fields with an array type, not at the root level.

Based on the architecture of your application how I can go directly to nested filed to get array because I got the below error once I am trying to parse the actual JSON retrieved from API.

Error: typeMismatch(Swift.Array, Swift.DecodingError.Context(codingPath: [], debugDescription: "Expected to decode Array but found a dictionary instead.", underlyingError: nil))

nalexn commented 4 years ago

Hey, I'd be able to help if you provide a concrete json example and the data structures you currently use for parsing. This might be helpful as well: encoding and decoding custom types

ahmadabudawod commented 4 years ago

Thanks @nalexn for your quick response, I have found how I can go to nested items from web-repository to be mapped with data structure at interactors. Below the line code that I have updated in your great sample app.

.sinkToLoadable { weakAppState?[\.userData.items] = $0.map { (**$0.records ?? []**) }}

nalexn commented 4 years ago

Great! Feel free to open a new issue if something else pops up