How can I add a `view` type to my identifiable data?

181 views Asked by At

I have the following data which is my identifiable. What I am trying to do is that I want to add a type of view as well so later I can give a Button that will link to those views. I don't know how to accomplish this. It says it doesn't accept view type.

My aim is to create cards and each card will hold Datum values. I want to set a Button to each of them that will take the user to that specific view. Since I do a ForEach, I want to be able to add each Datum's view. I did the following but swift didn't accept it and threw error.

Here is the code:

struct Datum:Identifiable {
    var id = UUID()
    var subtitle:String
    var name:String
    var footnote: String
    var description:String
    var image:String
    var categoryTag:String
    var comingSoon:Bool
    //var modelName: **View**
}

public var categoriesList = ["DL", "TSA", "NLP"]

var ModelsData:[Datum] = [
    Datum(subtitle: "Deep Learning",
         name: "Mask Detection",
         footnote: "An image classificaton model.",
         description: "This model classifies whether a person has a mask        or not. You simply take your camera and then take your face. Then, it will go and scan it. After that, you will see that at the bottom it shows green (means you have a mask) or red (which means you dont have a mask.)",
         image: "MaskDetectionImage",
         categoryTag: "DL",
         comingSoon: false,
         //modelName: MaskDetectionView()),      //I want to add a view
    
    Datum(subtitle: "Deep Leaning",
         name: "Video games",
         footnote: "An image classificaton app",
         description: "This model classifies whether a person has a mask or not. You simply take your camera and then take your face. Then, it will go and scan it. After that, you will see that at the bottom it shows green (means you have a mask) or red (which means you dont have a mask.)",
         image: "latest_1",
         categoryTag: "DL",
         comingSoon: false,
         //modelName: ContentView())] //Another different view

Here is image:

I defined: Struct defining

The error: Error

2

There are 2 answers

0
Cenk Bilgen On

This will give you some idea of how to start structuring it and how to keep the Datum model independent of SwiftUI (or however else you want to present it, save it etc).

struct Datum: Identifiable {
  let name: String
  var id: String { name }
}

struct ContentView: View {
  @State var selectedDatum: Datum?
  let datums: [Datum] = [
    Datum(name: "abc"), Datum(name: "xyz")
  ]
  var body: some View {
    ForEach(datums) { datum in
      Text(datum.name)
        .onTapGesture {
          selectedDatum = datum
        }
    }
    .fullScreenCover(item: $selectedDatum) { datum in
       Color.green
        .overlay(Text(datum.name).font(.headline))
      .onTapGesture {
        selectedDatum = nil
      }
    }
  }
}
7
ios coder On

Here I made an example of what you need, let me know if you need explaining.

import SwiftUI


struct Datum {
    var modelName: AnyView
}


struct ContentView: View {
    
    
    var datum: Datum = Datum(modelName: AnyView(Text("Omid").bold()) )
    
    
    var body: some View {

        datum.modelName
     
    }
}

Here:

import SwiftUI

struct ContentView: View {
    
    @State var ArrayOfDatum: [Datum] = ModelsData
    
    
    var body: some View {
        
        
        Divider()
        
        ForEach (0 ..< ArrayOfDatum.count) { index in
            Text(ArrayOfDatum[index].name)
            ArrayOfDatum[index].modelName
            Divider()
        }.font(Font.largeTitle.bold())
        
        
    }
}

struct Datum:Identifiable {
    var id = UUID()
    var subtitle:String
    var name:String
    var footnote: String
    var description:String
    var image:String
    var categoryTag:String
    var comingSoon:Bool
    var modelName: AnyView
}

public var categoriesList = ["DL", "TSA", "NLP"]

var ModelsData: [Datum] = [
    Datum(subtitle: "Deep Learning",
         name: "Mask Detection",
         footnote: "An image classificaton model.",
         description: "This model classifies whether a person has a mask        or not. You simply take your camera and then take your face. Then, it will go and scan it. After that, you will see that at the bottom it shows green (means you have a mask) or red (which means you dont have a mask.)",
         image: "MaskDetectionImage",
         categoryTag: "DL",
         comingSoon: false, modelName: AnyView(Image(systemName: "person")))
         //modelName: MaskDetectionView()),      //I want to add a view
    
    ,Datum(subtitle: "Deep Leaning",
         name: "Video games",
         footnote: "An image classificaton app",
         description: "This model classifies whether a person has a mask or not. You simply take your camera and then take your face. Then, it will go and scan it. After that, you will see that at the bottom it shows green (means you have a mask) or red (which means you dont have a mask.)",
         image: "latest_1",
         categoryTag: "DL",
         comingSoon: false, modelName: AnyView(Image(systemName: "gamecontroller")))
         //modelName: ContentView())] //Another different view
]

enter image description here