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

I want to have a list in my watchOS app which allows user to select one row at a time. The row should maintain its selected state.

The List works well, but I am currently using my own tap gesture recogniser: .onTapGesture() to select a row from my list, I also have to manage selection flow myself. I think it should be possible to simplify my code.

I have found following List API in the Apple documentation, exactly what I need: init(selection: Binding<SelectionValue?>?, content: () -> Content).

Creates a list with the given content that supports selecting a single row.

I face the compilation error trying to use this init:

'init(_:selection:rowContent:)' is unavailable in watchOS.

The method is marked as watchOS 6.0+ in the documentation. My deployment target is set to watchOS 6.2.

Here's full code:

Code Block swift
@State var selectedItem: CustomIdentifiableHashableStruct?
var body: some View {
List(dataController.dataSource, selection: $selectedItem) { item in
CustomHashableView(item: item)
}
}


Thanks for reading.




  • I'm facing the same problem ... do you find any solution?

    Thanks.

  • Same issue on watchOS 10 beta. Any suggestions to solve this?

  • watchOS 10 supports selection but this is the error message shown if there are any mistakes in the type of the $selection binding. It must be an optional and conform to Identifiable.

Add a Comment

Replies

Hello, Boris,

In your dataSource, did you mark your struct as…?

Code Block
struct dataSource: Codable, Equatable, Identifiable, Hashable {
[…]
}


Kind regards ;-)
Hi @haptic.

My data source looks following:

Code Block swift
@Published var dataSource = [CustomIdentifiableHashableStruct]()
struct CustomIdentifiableHashableStruct: Identifiable, Hashable {
// Contains bunch of string lets
}


I have tried to add Codable, Equatable conformance but it didn't fixed the compilation error.