gpt4 book ai didi

ios - 编译器无法在合理的时间 SWIFTUI 中对该表达式进行类型检查?

转载 作者:行者123 更新时间:2023-11-28 23:26:46 24 4
gpt4 key购买 nike

我们正在尝试使用SwiftUI创建GridView,我们遇到了问题,编译器想要破坏表达式,但我们不知道它到底发生在哪里。以下代码取自 https://github.com/johnsusek/FlowStack

没有支持来解决此问题。我们想让它发挥作用。

[![import SwiftUI

@available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
public struct FlowStack<Content>: View where Content: View {
// The number of columns we want to display
var columns: Int
// The total number of items in the stack
var numItems: Int
// The alignment of our columns in the last row
// when they don't fill all the column slots
var alignment: HorizontalAlignment

public let content: (Int, CGFloat) -> Content

public init(
columns: Int,
numItems: Int,
alignment: HorizontalAlignment?,
@ViewBuilder content: @escaping (Int, CGFloat) -> Content) {
self.content = content
self.columns = columns
self.numItems = numItems
self.alignment = alignment ?? HorizontalAlignment.leading
}

public var body : some View {
// A GeometryReader is required to size items in the scroll view
GeometryReader { geometry in
let frameWidth : CGFloat = geometry.size.width/CGFloat(self.columns)
let contentIndex : Int = (self.numItems / self.columns) * self.columns
// Assume a vertical scrolling orientation for the grid
ScrollView(Axis.Set.vertical) {

// VStacks are our rows
VStack(alignment: self.alignment, spacing: 0) {
let count = (self.numItems / self.columns)
ForEach(0 ..< count) { row in

// HStacks are our columns
HStack(spacing: 0) {
let innerCount = (self.columns - 1)
ForEach(0 ... innerCount) { column in
self.content(
// Pass the index to the content
(row * self.columns) + column,
// Pass the column width to the content
frameWidth
)
// Size the content to frame to fill the column
.frame(width: frameWidth)
}
}
}

// Last row
// HStacks are our columns
HStack(spacing: 0) {
let count = (self.numItems % self.columns)
ForEach(0 ..< count) { column in
self.content(
// Pass the index to the content
contentIndex + column,
// Pass the column width to the content
frameWidth
)
// Size the content to frame to fill the column
.frame(width: frameWidth)
}
}
}
}
}
}
}

] 1 ] 1

最佳答案

正如@Mojtaba Hosseini 所建议的那样,ViewBuilders 将不接受函数构建器内的变量声明。我通过创建全局变量进行了修改。请仔细阅读。

import SwiftUI

@available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
public struct FlowStack<Content>: View where Content: View {
// The number of columns we want to display
var columns: Int
// The total number of items in the stack
var numItems: Int
// The alignment of our columns in the last row
// when they don't fill all the column slots
var alignment: HorizontalAlignment

var count : Int
var count1 : Int
var innerCount : Int
var contentIndex : Int
var vSpacing : CGFloat = 0.0
var hSpacing : CGFloat = 0.0
var sizeOfItem : Int

public let content: (Int, CGFloat) -> Content

public init(
columns: Int,
numItems: Int,
alignment: HorizontalAlignment?,
@ViewBuilder content: @escaping (Int, CGFloat) -> Content) {
self.content = content
self.columns = columns
self.numItems = numItems
self.alignment = alignment ?? HorizontalAlignment.leading
sizeOfItem = self.numItems / self.columns
contentIndex = sizeOfItem * self.columns
innerCount = self.columns - 1
count1 = self.numItems % self.columns
count = self.numItems / self.columns
}

public var body : some View {
// A GeometryReader is required to size items in the scroll view
GeometryReader { geometry in
// Assume a vertical scrolling orientation for the grid
ScrollView(Axis.Set.vertical) {
// VStacks are our rows
VStack(alignment: self.alignment) {
ForEach(0 ..< self.count) { row in
// HStacks are our columns
HStack() {
ForEach(0 ... self.innerCount,id: \.self) { column in
self.content(
row * self.columns + column,
geometry.size.width/CGFloat(self.columns)
).frame(width: geometry.size.width/CGFloat(self.columns))
// Size the content to frame to fill the column
}
}
}
// Last row
// HStacks are our columns
HStack() {
ForEach(0 ..< self.count) { column in
self.content(
// Pass the index to the content
self.contentIndex + column,
// Pass the column width to the content
geometry.size.width/CGFloat(self.columns)
)
// Size the content to frame to fill the column
.frame(width: geometry.size.width/CGFloat(self.columns))
}
}
}
}
}
}
}

关于ios - 编译器无法在合理的时间 SWIFTUI 中对该表达式进行类型检查?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58444418/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com