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

SOLVED: How to create a List from a binding?

Forums > SwiftUI

Good evening, this is my first post here and thank you for the great website, being a Swift newbie I visit it often.

I have a simple test project at the Github, which works fine and displays a List/ForEach:

screenshot

After reading the article How to create a List or a ForEach from a binding I am trying to get rid of the ForEach in my ContentView:

            List ($vm.currentGames) { $gameNumber in
                NavigationLink(
                        destination: GameView(gameNumber: gameNumber)
                    ) {
                    Text("Game #\(gameNumber)")
                }
            }

But unfortunately I get the error:

Cannot convert value of type 'Binding<[Int]>' to expected argument type 'Range<Int>'

Please give a hint, why would here a Range be expected?

Thank you Alex

2      

Are you using Xcode 13? The feature Paul discusses in that article is new to iOS 15, which requires Xcode 13.

2      

Yes, Xcode 13.1 and iPhone 11 with iOS 15.0.2.

Should I maybe change something in my Xcode project?

Screenshot: https://imgur.com/FvtKnLF

2      

Try this:

List($vm.currentGames, id: \.self) { $gameNumber in

GamesViewModel.currentGames is an array of Int, which does not conform to Identifiable. Fixing that by explicitly telling the compiler how to uniquely identify each item should solve your issue.

Paul didn't have to do that in his article because his User struct is Identifiable.

3      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

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.