swiftswiftuiios16

SwiftUI 4 List initializer not available in iOS


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?


Solution

  • 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