Home > OS >  SwiftUI: Referencing Model Values in Nested Optional Arrays
SwiftUI: Referencing Model Values in Nested Optional Arrays

Time:12-05

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:

  "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.?

CodePudding user response:

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)
                }
            }
        }
    }
}
  • Related