|
Hi, I am in need of some help on how to interact with an API that provides a response as a dictionary which looks like this... (DictionaryResponseSample.json)
I have no problem directly importing into Core Data when the response is an array as in... (ArrayResponseSample.json)
The code driving this is from Donny Wals Core Data book From DataImporter.swift
Called in ContentView.swift...
I'd like to keep the response metadata (success) in the API so I can confirm the success of a response. I could omit 'success' on the successul API call and use Swift's error handling to determine if the response responded with a success and only return the failure metadata on a failed API call. It'd be great to just 'slice' off the paylaod array as I could resuse an API response class, but I cannot figure out how to do that I believe that I am trying to do is very similar to Paul's Whitehouse project here: https://www.hackingwithswift.com/read/7/3/parsing-json-using-the-codable-protocol with the difference being using NSManagedObject classes instead of just Codable classes. This would be a big breakthrough for me so any help would be greatly appreciated. Thanks in advance! |
|
Instead of using Donny Wals' generic importer , I suggest you try defining this struct and decoding it:
|
|
I figured out how to create a class for API responses that contains a generic. The allows me to receive API responses from a single class regardless of the type of data being returned, as long as I know what type is being returned. It looks like this.... ApiHandler.swift
ApiResponse.swift
And it can be called like this...
I pulled a lot of this from: https://stackoverflow.com/questions/48657076/return-a-generic-swift-object-from-a-rest-api-client It shouldn't be difficult to extend to NSManagedObjects |
SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.
Sponsor Hacking with Swift and reach the world's largest Swift community!
This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.