Core data 如何在SwiftUI的PreviewProvider中编写示例模型对象?
我有一些代码,但不是全部。我了解关于SwiftUI和核心数据的基本代码,但不知道如何在下面的代码中编写redFox模型示例 我试图自己编写redFox代码,但没能做到Core data 如何在SwiftUI的PreviewProvider中编写示例模型对象?,core-data,swiftui,Core Data,Swiftui,我有一些代码,但不是全部。我了解关于SwiftUI和核心数据的基本代码,但不知道如何在下面的代码中编写redFox模型示例 我试图自己编写redFox代码,但没能做到 import SwiftUI struct AnimalCell : View { let model: AnimalCellModel var body: some View { HStack { Text(model.image)
import SwiftUI
struct AnimalCell : View
{
let model: AnimalCellModel
var body: some View
{
HStack
{
Text(model.image)
Text(model.commonName)
Text(model.familyName)
Text(model.scientificName)
}
}
}
#if DEBUG
public enum AnimalCellPreviews : PreviewProvider
{
public static var previews: some View
{
AnimalCell(model: .redFox)
}
}
#endif
我应该看到预览,但无法看到,因为代码不完整。不确定这是否是您要求的,但标准预览代码如下所示:
#if DEBUG
struct ContentView_Previews : PreviewProvider {
static var previews: some View {
ContentView()
}
}
#endif
因此,在您的情况下,应该是:
#if DEBUG
struct AnimalCell_Previews : PreviewProvider {
static var previews: some View {
AnimalCell(model: RedFox())
}
}
#endif
假设您有一个RedFox结构或类不确定这是否是您想要的,但标准预览代码如下所示:
#if DEBUG
struct ContentView_Previews : PreviewProvider {
static var previews: some View {
ContentView()
}
}
#endif
因此,在您的情况下,应该是:
#if DEBUG
struct AnimalCell_Previews : PreviewProvider {
static var previews: some View {
AnimalCell(model: RedFox())
}
}
#endif
假设您有一个RedFox结构或类,则需要将
RedFox
作为静态属性添加到AnimalCellModel
:
extension AnimalCellModel {
static var redFox = AnimalCellModel(...)
}
然后您可以将其与示例中所示的
.redFox
语法一起使用。您需要将redFox
作为静态属性添加到AnimalCellModel
:
extension AnimalCellModel {
static var redFox = AnimalCellModel(...)
}
然后,您可以将其与示例中所示的.redFox
语法一起使用。不,它应该是:AnimalCell(model:.redFox)。如何编写.redFox而不是redFox()代码,假设.redFox是一个结构或类。假设.redFox是一个结构或类,它的代码是什么样子的?不,应该是:AnimalCell(model:.redFox)。如何编写.redFox而不是redFox()代码,假设.redFox是一个结构或类。假设它是一个结构或类,那么.redFox的代码看起来像什么?