编译器无法在合理的时间 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)
}
}
}
}
}
}
}
]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))
}
}
}
}
}
}
}
我们正在尝试使用 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))
}
}
}
}
}
}
}