UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Niggling CheckoutView warning in iDine project I would like to resolve.

Forums > SwiftUI

Firstly I would like to say thank you for the excellent iDine sample project! Superb! I've implemented it using Xcode 11.4 beta 3 and whilst doing so, learned heaps about SwiftUI.

However, when I run the project and select a tip amount on the CheckoutView view, I get a Xcode warning message in the console which really bugs me. (This warning also popped up when I clicked the "Confirm Order' button.) The warning does not affect the functionality of the app, but as I'm sure you can appreciate, that's not really the point.

Whenever I click a tip amount, using the Picker with the SegmentedPickerStyle(), Xcode produces the following warning:

ForEach<Range<Int>, Int, Text> count (3) != its initial count (1). `ForEach(_:content:)` should only be used for *constant* data. Instead conform data to `Identifiable` or use `ForEach(_:id:content:)` and provide an explicit `id`!

I've searched google (StackOverflow), tried a bunch of things using different ForEach initialisers, changed/replaced static let tipamounts = [ ... ] with various other data types, including an array of Identifiable struct that contains let id: Int; let amount: Int..., but nothing has helped. The warning continues to be printed each time I click a tip amount. *(What is interesting though is that when I go to the CheckoutView from the OrderView using the Place Order navlink, the view is always rendered without the warning, though the default @State var tipAmount = 1 (i.e. is not 0).

With all the things I've tried and Xcode's weird error messages when views aren't quite right, I'm almost of the opinion that the problem may be somewhere else in the view but don't know where. (With respect to this question, my implementation of CheckoutView is as per the tutorial.)

Does anyone know how to resolve this issue?

PS.. I have extended the project to use MVVM instead of MV and used @ObservedObject instead of @EnvitormentObject throughout.

2      

Hi I was reviewing my iDine and found getting same error.

You need to put , id: \.self after 0 ..< Self.tipAmounts.count in the ForEach

EG

ForEach(0 ..< Self.tipAmounts.count, id: \.self) {
          Text("\(Self.tipAmounts[$0])%")
}

This should quieten the warning

PS I found that i had to do this with the ForEach(0 ..< Self.paymentTypes.count, id: \.self) so if you have any other ForEach you will have to the same for them.

3      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.