Xcode 操场上的 SwiftUI 和 foreach 问题 [英] Problem with SwiftUI and foreach on Xcode playground

查看:29
本文介绍了Xcode 操场上的 SwiftUI 和 foreach 问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 SwiftUI 中执行一个简单的代码,但它显示错误:执行被中断,原因:信号 SIGABRT.这是一个代码`

struct ContentView: View {让数据 = (1...100).map { "Item \($0)";}让列 = [GridItem(.adaptive(最小值:80))]var主体:一些视图{滚动视图{LazyVGrid(列:列,间距:20){ForEach(data, id: \.self) { item in文本(项目)}}.padding(.水平)}.frame(最大高度:300)}}

解决方案

在使用 ForEach 时,Playground(至少是这个版本)似乎存在一个错误.我有同样的问题,您可以在 console

的 CrashLogs 中找到更多详细信息

检查使用 ForEach 崩溃的操场

解决方法

  • 将 ContentView 移动到 Playground Sources 中的单独文件
  • 不要忘记公共修饰符
<预><代码>公共结构内容视图:查看{让数据 = (1...100).map { "Item \($0)";}让列 = [GridItem(.adaptive(最小值:80))]公共初始化(){}公共变量体:一些视图{滚动视图{LazyVGrid(列:列,间距:20){ForEach(data, id: \.self) { item in文本(项目)}}.padding(.水平)}.frame(最大高度:300)}}

i am trying to execute a simple code in SwiftUI but it shows error: Execution was interrupted, reason: signal SIGABRT. here is a code `

struct ContentView: View {

    let data = (1...100).map { "Item \($0)" }

    let columns = [
        GridItem(.adaptive(minimum: 80))
    ]
    var body: some View {
        ScrollView {
            LazyVGrid(columns: columns, spacing: 20) {
                ForEach(data, id: \.self) { item in
                    Text(item)
                }
            }
            .padding(.horizontal)
        }
        .frame(maxHeight: 300)
    }
}

解决方案

It seems there is a bug with (at least this version) of Playground, when using ForEach. I have the same issue and you can find more details in the CrashLogs of console

Check crashing playground with ForEach

Workarround

  • Move ContentView to a separate file in Sources of Playground
  • Don't forget the public modifiers


public struct ContentView: View {

    let data = (1...100).map { "Item \($0)" }

    let columns = [
        GridItem(.adaptive(minimum: 80))
    ]
    
    public init() {}
    
    public var body: some View {
        ScrollView {
            LazyVGrid(columns: columns, spacing: 20) {
                ForEach(data, id: \.self) { item in
                    Text(item)
                }
            }
            .padding(.horizontal)
        }
        .frame(maxHeight: 300)
    }
}

这篇关于Xcode 操场上的 SwiftUI 和 foreach 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆