-1

我有以下可识别的数据。我想做的是我也想添加一个typeview以便稍后我可以提供一个Button链接到这些视图的链接。我不知道如何做到这一点。它说它不接受视图类型。

我的目标是制作卡片,每张卡片都会保存Datum价值。我想为Button它们中的每一个设置一个,将用户带到该特定视图。因为我做了一个ForEach,我希望能够添加每个Datum的视图。我做了以下但swift不接受它并抛出错误。

这是代码:

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

这是图像:

我定义: 结构定义

错误: 错误

4

2 回答 2

0

在这里,我举了一个你需要什么的例子,如果你需要解释,请告诉我。

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
     
    }
}

这里:

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
]

在此处输入图像描述

于 2020-11-24T17:50:00.983 回答
0

这将使您了解如何开始构建它以及如何保持 Datum 模型独立于 SwiftUI(或者您想要呈现它、保存它等)。

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
      }
    }
  }
}
于 2020-11-25T19:37:59.187 回答