|
Hey everyone, Long time luker but I have a question that I am sure is simple but having a tough time to find a solution for (probably because I don't know how to phrase it correctly). Basically I have a JSON that is nested but that isnt a huge deal and I have the model working for it at this point. My challenge is the lowest level of the JSON has a few arrays that have just the values in them but not a seperate dictionary key and I am not sure how to model that correctly so I can accsess the data. An example of the dictionary looks like this... { "records": [ { "id": "String", "fields": { "Name": "String", "Date Received": "String", "Request Type": [ "String" ], "Email/Phone Number": "String", }, "createdTime": "2021-02-22T16:22:55.000Z" } ] } Normally I would assume I need to make another struct for the arrayed property but since it only contains the values and strings (when more than one) I don't have a key : value pattern to use for the struct. |
|
First, that's not valid JSON because of this: Clear that up, and the key indicated by |
|
Sorry you can assume the comma is gone already (Because it is) that was just a bad copy over on my part. And that is the issue there is no key indicated for Request Type. The only thing in the array is the actual values so it reads requestType : [ "One", "Two", "Three" ] but there isn't a key associated with it. Here is my structs for the codables if this helps.
The issue I am running into is that while the network call gets the data and it processes and I can see it in the preview. Certain fields like the requestType give me a can not complie in time error that wont resolve unless I don't use them. So am I just accessing them wrong? Right now I am using a ForEach to display the needed data and the general code is Text(index.fields.name ?? "") and it works fine for anything that is not the key : [value] setup in the bottom layer of the JSON. |
SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th. Sponsor Hacking with Swift and reach the world's largest Swift community! |
|
No, you've got it right in your struct.
Something like that. Point is, it's just an array, so treat it like an array. |
|
Right now this is what I have as my for each
And name works (that's why it's listed twice right now) but if I change it to
It won't complie anymore. |
|
Right, because You need an inner |
|
So another for each within the stack that would refrence specifically that option?
Or as an if let above the var body: any View { that I refrence in the stack? |
|
Something in the
|
|
Okay. Well good news is that it works :) so thank you for the help. Now I just need to educate my self more on why lol. It seems like there should be a way to just note it via the struct in a way so that when the decoder is done it is avalible without another for each but hey if it works it works so thank you. |
|
I mean, it's an array. How else would you expect to access its values without looping through them? I suppose if you just wanted to use one value out of the array you could subscript into it like If you know that Or you could add a computed property to your
and then in your
But that would give you an empty It really all depends on how you would want to handle the case where |
|
Yeah that all makes sense. I just mean it seems like the decoder should be able to see its an array based on the struct and loop through those values on its own with the network call or first pass using the for each but its all good. I am sure there are very good technical reasons why it does not do that own its own (though it does loop through the array on its own with the other arrays that are higher up in the process) so it's all good and thank you for the help. In the case of the other values since they are all optional I just added the ?? "If nil text here" so I just kind of assumed that swift would say soemthing like "hey there is no values in the array this time so use the if nil text" like it did with the other string values but as I am still VERY much learning I am fine with just learning how it is designed to work not how I would expect it to work. |
|
Just a clarification: The You can create a data structure that conforms to Hope that helps explain why things work the way they do. |
SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.
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.