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

 

Related articles

Andrew Fletcher12 Aug 2022
Using SwiftUI URLComponent to change a URL's scheme
The challenge I was facing, I had written a script to scan barcodes and use Google book API to view the contents. &nbsp;However, a snippet of the JSON response { "contentVersion": "0.2.0.0.preview.0", "panelizationSummary": { "containsEpubBubbles": false, ...
Andrew Fletcher19 Jul 2022
Xcode NullInjectorError
In Xcode and executing a run command, the response error was: { "name": "NullInjectorError", "ngTempTokenPath": null, "ngTokenPath": ["Ee", "fe", "Ne", "Ne", "Ne"] } This was a result of me attempting to incorrectly import the AppComponent from inside a service. import {...