Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

SwiftUI: Referencing Model Values in Nested Optional Arrays

In my app’s view, I’m trying to list out the values of an array inside another array. The arrays are being filled via JSON from an API call. Both arrays are optionals. I first check to see if the array contains a nil value. I know in this case that it’s not nil.

I’m getting an error that "Value of type ‘[Landscape]’ has no member ‘mulching’ "

Here’s the applicable portion of the JSON that’s present:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  "projectType": {
        "landscapes": [
            {
                "id": 6,
                "types": [
                    "mulching",
                    "clearing",
                    "planting",
                    "treeCare",
                    "other"
                ],
                "mulching": {
                    "id": 1,
                    "mulchingType": "Create new mulch beds",
                    "currentlyInPlace": "Grass / Weeds",
                    "currentlyInPlaceCustom": "",
                    "roomLength": "20",
                    "roomWidth": "4",
                    "color": "Black",
                    "customColor": "",
                    "length": "",
                    "approximateLength": ""
                }
              }
            }

My model is structured as follows:

// MARK: - ProjectType
struct ProjectType: Codable {
    let landscapes: [Landscape]?
    let generalConstructions: [GeneralConstruction]?
}

// MARK: - Landscape
struct Landscape: Codable {
    let id: Int
    let types: [String]
    let mulching: Mulching?
    let clearing: Clearing?
    let planting: Planting?
    let treeCare: TreeCare?
    let other: Other?
}

// MARK: - Mulching
struct Mulching: Codable {
    let id: Int
    let mulchingType, currentlyInPlace, currentlyInPlaceCustom, roomLength: String
    let roomWidth, color, customColor, length: String
    let approximateLength: String
}

Here’s where I’m running into an error:

import SwiftUI

struct LandscapeSpecificsView: View {
    
    let projectType: ProjectType
    
    var body: some View {
        VStack{
            if let landscapes = projectType.landscapes {
                if landscapes.contains(where: {$0.mulching != nil}) {
                    Text(
                        "\(landscapes.mulching)" **ERROR HERE**
                    )

                }
            }
        }

    }
}

How do I reference the mulching model values like mulching.id, mulching.mulchingType, mulching.color, etc.?

>Solution :

Reiterating what @jnpdx said,
in your code landscapes is an array of Landscape, and each element contains mulching,
but not the array itself. You could try something like this:
(making struct Mulching Identifiable)

struct LandscapeSpecificsView: View {
    
    let projectType: ProjectType
    
    var body: some View {
        VStack{
            if let landscapes = projectType.landscapes,
               let mulchings = landscapes.compactMap{$0.mulching} {
                ForEach(mulchings) { mulch in
                    Text(mulch.mulchingType)
                }
            }
        }
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading