[ad_1]
I need to parse the next JSON into a listing with SwiftUI, nonetheless I’m experiencing some points with this.
JSON (there are lots of extra entires than this with random Identifiers because the entry. That is what triggered it to be a lot tougher to carry out):
{
"DCqkboGRytms": {
"title": "graffiti-brush-3.zip",
"downloads": "5",
"views": "9",
"sha256": "767427c70401d43da30bc6d148412c31a13babacda27caf4ceca490813ccd42e"
},
"gIisYkxYSzO3": {
"title": "twitch.png",
"downloads": "19",
"views": "173",
"sha256": "aa2a86e7f8f5056428d810feb7b6de107adefb4bf1d71a4ce28897792582b75f"
},
"bcfa82": {
"title": "PPSSPP.ipa",
"downloads": "7",
"views": "14",
"sha256": "f8b752adf21be6c79dae5b80f5b6176f383b130438e81b49c54af8926bce47fe"
}
}
SwiftUI Codables:
struct Feed: Codable {
let checklist: [FeedValue]
}
struct FeedValue: Codable, Identifiable {
var id = UUID()
let title, downloads, views, sha256: String
}
JSON Fetch methodology (and parse):
func getFeeds(completion:@escaping (Feed) -> ()) {
// URL hidden to respect API privateness
guard let url = URL(string: "") else { return }
URLSession.shared.dataTask(with: url) { (knowledge, _, _) in
let feed = strive! JSONDecoder().decode(Feed.self, from: knowledge!)
DispatchQueue.fundamental.async {
completion(feed)
}
}.resume()
}
Lastly, the view containing my ForEach loop:
struct HomeView: View {
@State non-public var scrollViewContentOffset: CGFloat = .zero
@State var listFeed = Feed(checklist: [])
var physique: some View {
// Z-coord standing bar holder
ZStack(alignment: .high) {
TrackableScrollView(.vertical, showIndicators: true, contentOffset: $scrollViewContentOffset) {
// Public information Begin
LazyVStack(spacing: 0) { {
// Withholds our content material for every asset retrieved.
// Essential half
ForEach(listFeed.checklist, id: .id) { obtain in
AssetView(title: .fixed(obtain.title), downloads: .fixed(obtain.downloads), views: .fixed(obtain.views))
}
}.padding([.leading, .top, .trailing], 25)
// Public information Finish
}.edgesIgnoringSafeArea([.leading, .trailing, .top])
}.background(Coloration.backgroundColor.ignoresSafeArea())
.onAppear() {
getFeeds { feed in
//print(feed) : this did occur to work on the time of testing
self.listFeed = feed
}
}
}
}
And for the error that’s being offered:
Starfiles/HomeView.swift:32: Deadly error: ‘strive!’ expression unexpectedly raised an error: Swift.DecodingError.keyNotFound(CodingKeys(stringValue: “checklist”, intValue: nil), Swift.DecodingError.Context(codingPath: [], debugDescription: “No worth related to key CodingKeys(stringValue: “checklist”, intValue: nil) (“checklist”).”, underlyingError: nil))
I’ve spent a number of hours at the moment altering the strategy of parsing, however was unable to discover a answer alone.
EDIT: I wish to point out that I want a technique to entry the dictionary ID’s as properly, as they will be used for getting into in a brand new view.
[ad_2]
