![](/img/trans.png)
[英]The compiler is unable to type-check this expression in a reasonable time in SwiftUI?
[英]The compiler is unable to type-check this expression in reasonable time SWIFTUI?
我们正在尝试使用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)
}
}
}
}
}
}
}
正如@Mojtaba Hosseini 建议的那样, ViewBuilders将不接受 function 构建器中的变量声明。 我通过创建全局变量进行了修改。 请通过它 go。
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))
}
}
}
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.