21

When creating a list, previous versions of Xcode allowed for the following on iOS:

List(tabs, selection: $lastTab) {tab in
…
} 

This now gives an error:

'init(_:selection:rowContent:)' is unavailable in iOS

also true for

List(tabs, id:\.self, selection: $lastTab)

and other variations such as:

List(selection: $lastTab) {
   ForEach(tabs, id:\.self) {tab in 
...
}

Has anyone else run into this problem?

Ferdinand Rios
  • 972
  • 6
  • 18
  • I have the same issue, but in my case, the editor gave me a "fix" option that inserted an unusual looking autocomplete for the list. – Stoic Jul 09 '22 at 00:09
  • @Stoic What was that "fix"? – Ferdinand Rios Jul 09 '22 at 04:06
  • Apple's tutorial from 2022 is having the same error https://developer.apple.com/tutorials/swiftui-concepts/defining-the-source-of-truth-using-a-custom-binding – Jonny Feb 02 '23 at 05:31

1 Answers1

50

You probably forgot to make selection as Optional, ie.

@State private var lastTab: Int? = 0   // << here !! `Int?`

List(tabs, selection: $lastTab) {tab in
// ...
}

Verified with: Xcode 14b3 / iOS 16

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Asperi
  • 228,894
  • 20
  • 464
  • 690
  • 7
    The error message is misleading. – winner.ktw Mar 04 '23 at 14:26
  • Be aware that this might lead to issues with `.sheet`/`.popover`! For me using an optional for the `TabView` selection always switched back to the first tab when presenting the sheet. To workaround this I’ve bridged the optional binding to a non-optional binding. – alexkaessner Jun 23 '23 at 08:15