So I have a Binding declared inside my view and I can't seem to get it to work when generating a preview, and it keeps crashing.
So I have the following view:
struct MapPinSheetSectionOneView: View {
// Safari View - Binding
@Binding var isBrowsingWebsite: Bool
// Variables
let item: Place
// Main body
var body: some View {
HStack(alignment: .top, spacing: 0) {
// Type/Title/Excerpt
showSectionOne
.border(.blue)
// Spacers
Spacer()
Spacer()
Spacer()
// Link & Close Button
showSheetLinkAndCloseButton
.border(.red)
}
.border(.red)
// Divider
Divider()
}
// MARK: Category, Title & Excerpt
private var showSectionOne: some View {
VStack(alignment: .leading, spacing: 0) {
Group {
if item.category != "" {
Text(verbatim: item.category)
.padding(8)
.background(
RoundedRectangle(cornerRadius: 8)
.fill(Color.accentColor)
)
}
if item.title != "" {
Text(item.title)
.font(.title2.weight(.semibold))
}
if item.excerpt != "" {
HStack(alignment: .top, spacing: 3) {
Text(item.excerpt)
.font(.footnote)
.foregroundColor(.secondary)
.lineLimit(2)
Spacer()
}
}
}
.lineLimit(1)
}
}
// MARK: Link & Close Button
private var showSheetLinkAndCloseButton: some View {
Group {
if item.website != "" {
Button(action: {
self.isBrowsingWebsite = true
}) {
Image(systemName: "link.circle.fill")
}
.sheet(isPresented: $isBrowsingWebsite) {
SafariViewWrapper(url: URL(string: item.website)!)
}
}
Image(systemName: "xmark.circle.fill")
}
.imageScale(.large)
.foregroundColor(.accentColor)
}
}
Then, I have the following attempted preview:
struct MapPinSheetSectionOneView_Previews: PreviewProvider {
@Binding var isBrowsingWebsite: Bool
static var previews: some View {
MapPinSheetSectionOneView(
isBrowsingWebsite: $isBrowsingWebsite,
item: Place(
id: 0,
title: "Title",
category: "Category",
type: "Type",
description: "Description",
excerpt: "Excerpt",
address: "Address",
city: "City",
state: "State",
zipcode: 0,
country: "Country",
lat: 39.828194,
long: -98.569611,
altitude: 0,
amenity: ("Amenities"),
admission: "Free",
website: "Website"
)
)
}
}
For some reason, it keeps crashing and I am getting the following errors:
Instance member '$isBrowsingWebsite' cannot be used on type 'MapPinSheetSectionOneView_Previews'
Does anyone know how to make a binding bool work inside previews?