Компилятор не может проверить это выражение в разумные сроки SWIFTUI?

Мы пытаемся создать GridView с помощью SwiftUI, возникает проблема, компилятор хочет разбить выражение, но мы не знаем, где именно это происходит. Следующий код взят из 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


person karthikeyan    schedule 18.10.2019    source источник
comment
У вас не может быть объявления в функции-строителе.   -  person Mojtaba Hosseini    schedule 18.10.2019
comment
Я изменил его на глобальный, но все еще не работает   -  person karthikeyan    schedule 18.10.2019


Ответы (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))
            }
          }
        }
      }
    }
  }
}
person karthikeyan    schedule 18.10.2019