Skip to main content

Creating a foreach loop such as 

var maxDigits: Int = 5

private var pinDots: some View {
    
    HStack {
        ForEach(0..<maxDigits) { index in
        ZStack {
          Image(systemName: self.getImageName(at: index))
            .font(.system(size: 50.0, weight: .thin, design: .rounded))
            .foregroundColor(.white)
            .background(
              index < pin.count ?
                Color.black.opacity(0.8) :
              .clear).cornerRadius(35.0)
        }
        .padding([.leading, .trailing], 4.0)
      }
    }
    
  }

The integer maxDigits is showing an error 'Non-constant range: argument must be an integer literal'

Even though a let or var in this instance may not be the same as a literal, its value can never change so it's constant.  Subsequently, the range created from from a literal when pass into ForEach.init(_:) are both the same constant range.

Alter the ForEach line as follows

ForEach(0..<maxDigits, id: \.self) { index in