-1

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

Yonus
  • 233
  • 2
  • 12

2 Answers2

0

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

ios coder
  • 1
  • 4
  • 31
  • 91
  • Thanks. I added my view instead of text and got this : `Static method 'buildBlock' requires that 'modelName' conform to 'View'` – Yonus Nov 24 '20 at 18:29
  • you have to add .modelName to end try it and tell me result – ios coder Nov 24 '20 at 18:37
  • 1. So I created a struct as you did. 2. Then I gave it to my identifiable -> `var modelName:Datum` 3. Then when defining each item, I added it as well. I have updated the post and added picture. Please check it out – Yonus Nov 24 '20 at 20:37
  • you code it wrong! you did twice! let me see if I can edit your code. – ios coder Nov 24 '20 at 20:47
  • The thing is that `fullpresentationcover` opens up a new `View` and `AnyView` != `View` – Yonus Nov 24 '20 at 20:56
  • I even used a dictionary/collection by which if `key` == `id`, then give me the `View`. Still, it gave me error of `Heterogeneous` ...... and must add `[Int: Any]` – Yonus Nov 24 '20 at 20:57
  • check my new code for your wrong coding way, hope help you – ios coder Nov 24 '20 at 21:05
0

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
      }
    }
  }
}
Cenk Bilgen
  • 1,330
  • 9
  • 8