swift - 实例成员 '$isBrowsingWebsite' 不能用于类型 'MapPinSheetSectionOneView_Previews'

标签 swift swiftui

因此,我在 View 中声明了一个 Binding,但在生成预览时似乎无法使其工作,并且它不断崩溃。

所以我有以下观点:

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

然后,我尝试进行以下预览:

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

由于某种原因,它不断崩溃,并且我收到以下错误:

Instance member '$isBrowsingWebsite' cannot be used on type 'MapPinSheetSectionOneView_Previews'

有人知道如何使绑定(bind) bool 在预览中工作吗?

最佳答案

方法 1 - 动态值(value)

为了在预览中使用绑定(bind)(无需将其声明为常量),您需要创建一个包装器。

我必须为 Place 创建一个结构,因为它没有包含在内。您可以将其替换为您的 Place 版本

struct PreviewWrapperWithState<Value, Content: View>: View {
@State var value: Value
var content: (Binding<Value>) -> Content

var body: some View {
    content($value)
}

init(_ value: Value, content: @escaping (Binding<Value>) -> Content) {
    self._value = State(wrappedValue: value)
    self.content = content
  }
}

struct MapPinSheetSectionOneView_Previews: PreviewProvider {
    static var previews: some View {
        var thingToPreview = false  // ERROR
        let place =  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"
        )
        
        PreviewWrapperWithState(thingToPreview) { MapPinSheetSectionOneView(isBrowsingWebsite: $0, item: place) }
    }
}

方法 2 - 恒定值

另一种方法是使用常量初始化您的绑定(bind)。这会将功能限制为其初始化状态。

struct MapPinSheetSectionOneView_Previews: PreviewProvider {
static var previews: some View {
    MapPinSheetSectionOneView(
        isBrowsingWebsite: Binding.constant(true),
        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"
        )
    )
  }
}

关于swift - 实例成员 '$isBrowsingWebsite' 不能用于类型 'MapPinSheetSectionOneView_Previews',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74502632/

相关文章:

ios - NSURL 不会从字符串初始化为远程字体文件 (.TTF)

swift - 如何在 Swift Package Manager 库中包含 Assets /资源?

SwiftUI 将列表中的行间距减少为空

ios - 如何使用SwiftUI在多个屏幕上获取键盘高度并移动按钮

swift - 如何将多个 SCNScene 添加到一个 SCNScene?

ios - 使用 ALAssetsLibrary-CustomPhotoAlbum 将照片保存在特定相册中

ios - 如何在一个类或 .swift 文件中编写一个方法并在整个代码中调用该方法?

ios - 如何将 `Observable` 转换为 `ControlEvent`

SwiftUI:如何在 macOS 应用程序中实现编辑菜单

swift - TextField 中的占位符文本颜色