首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >实例成员'$isBrowsingWebsite‘不能用于'MapPinSheetSectionOneView_Previews’类型

实例成员'$isBrowsingWebsite‘不能用于'MapPinSheetSectionOneView_Previews’类型
EN

Stack Overflow用户
提问于 2022-11-19 18:33:22
回答 1查看 28关注 0票数 1

因此,我在视图中声明了一个绑定,在生成预览时,我似乎无法让它工作,而且它一直在崩溃。

因此,我有以下看法:

代码语言:javascript
复制
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)
    }
}

然后,我尝试预览以下内容:

代码语言:javascript
复制
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"
            )
        )
    }
}

由于某种原因,它不断崩溃,我得到了以下错误:

实例成员'$isBrowsingWebsite‘不能用于'MapPinSheetSectionOneView_Previews’类型

有谁知道如何在预览中使绑定bool工作吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-11-19 20:41:21

方法1-动态值

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

我必须为Place创建一个结构,因为它是不包括的。你可以用你的版本的Place交换它

代码语言:javascript
复制
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-常数值

另一种方法是使用常量初始化绑定。这将将功能限制在其初始化状态。

代码语言:javascript
复制
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"
        )
    )
  }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74502632

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档