Reputation: 21
I have some code but I don't have all of it. I understand the basics of the code that I have regarding SwiftUI and Core Data but I don't know how to code the redFox model example in the following code.
I tried to code redFox myself but was unable to do so.
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
I should see the preview but can't because the code is incomplete.
Upvotes: 2
Views: 403
Reputation: 1249
You need to add redFox
as a static property to the AnimalCellModel
:
extension AnimalCellModel {
static var redFox = AnimalCellModel(...)
}
Then you can use it with the .redFox
syntax like shown in your example.
Upvotes: 1
Reputation: 3388
Not sure if this is what you're asking for but the standard preview code is as follows:
#if DEBUG
struct ContentView_Previews : PreviewProvider {
static var previews: some View {
ContentView()
}
}
#endif
So in your case it should be:
#if DEBUG
struct AnimalCell_Previews : PreviewProvider {
static var previews: some View {
AnimalCell(model: RedFox())
}
}
#endif
Assuming you have a RedFox struct or class
Upvotes: 0