1

very new to SwiftUI and I've managed to learn a lot in very little time. The one thing I'm struggling with is displaying an array from a JSON file using a loop. I would love if someone can help me out with this!

Sorry if this is a super n00b question, I've searched a lot and I just can't seem to find any examples or answers to how to display this (or I'm possibly trying the wrong things)

Here is a sample of my JSON object

{
"name": "Name of Spot",
"city": "City of Spot",
"state": "State of Spot",
"id": 1001,
"description": "Description of Spot",
"imageName": "imageName",
"categories": [
   {
   "category": "Category Name 1"
   },
   {
   "category": "Category Name 2"
   },
   {
    "category": "Category Name 3"
    }
   ]
}

Here is my current data model

struct Spot: Hashable, Codable, Identifiable {
    var id: Int
    var name: String
    fileprivate var imageName: String
    var city: String
    var state: String
    var description: String

What I would like to do is create a loop that displays a Text string of each of the Categories. I can't figure out how to add the array to my struct or how to create the loop that will display them. I've managed to create loops to get each of the "spots" and dynamically pull in the rest of the info, just not the categories.

Thanks in advance!

Edited: Here is an example of where I am getting the error

struct TestArray: View {
    
    var spot: Spot
    
    var body: some View {
        VStack {
            spot.categories.forEach { (category) in
                Text(category["category"] ?? "n/a")
            }
        }

    }
}
5
  • How are you deserializing the JSON into your Spot object? Commented Aug 27, 2020 at 14:47
  • I'm not exactly sure what you mean by deserializing, but this is the line that loads the json let spotData: [Spot] = load("spotData.json") Commented Aug 27, 2020 at 14:54
  • I'm asking how you are converting the JSON into your object, I basically want to know if you are using a JSONDecoder and decoding into your Spot struct. I'm guessing your load method is doing that and if so, the answer I posted should work. In the future, you should try to post a minimal example of your code, it's a lot more difficult to give you the proper answer when it's not the complete code and assumptions have to be made in how you're implementing other parts. Commented Aug 27, 2020 at 15:18
  • My apologies, I'm still learning all of this Commented Aug 27, 2020 at 15:34
  • It's all good, just trying to give some advice for future questions. I recommend giving this a read through to get a better idea of the dos and don'ts when asking questions. Commented Aug 27, 2020 at 15:55

1 Answer 1

1

You can make a Category object and declare categories as an array of that object. If you want to do it without making a new object that only has one property, you could also just use an array of Dictionaries with String as the key value.

struct Spot: Hashable, Codable, Identifiable {
    var id: Int
    var name: String
    fileprivate var imageName: String
    var city: String
    var state: String
    var description: String
    var categories: [Category]
}

struct Category: Codable {
    var category: String
}

If you want to use an array of Dictionaries instead:

struct Spot: Hashable, Codable, Identifiable {
    ...
    var categories: [[String: String]]
    ...
}

edit:

To iterate and display the categories from the array of Dictionaries, you should be able to do it something like this, assumming your Spot object is named spot:

VStack {
    spot.categories.forEach { (category) in
        Text(category["category"] ?? "n/a")
    }
}
Sign up to request clarification or add additional context in comments.

10 Comments

Thank you for this! How would you write the for loop for the dictionaries example to display text items for each of the categories?
Also, by "array of dictionaries", do you mean in the JSON file, categories would look like this? "categories": [ "Category 1", "Category 2" ]. Again, very sorry for the likely stupid questions, I'm learning as I go here
I updated my answer for looping through the dictionaries and displaying them.
'Array of dictionaries' is how your originally formatted JSON is formatted, in JSON they're just called 'objects' but they are more akin to a Dictionary in Swift where you have values accessed by a key. The example in your comment is an Array of Strings from a Swift perspective.
Not gonna lie, I'm not sure how to do that. Looks like I've got some more learning to do so I won't waste any more of your time with my n00bness haha. Thanks so much for helping me out, I'll happily mark your answer as accepted as I'm sure its my mistakes here haha
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.