Automatically Synthesize Identifiable Conformance for Enums

I think things could be addressed with small data model refactors that SwiftUI already demands in some cases. You earlier brought up this example:

enum MyTableCell {
  case aboutThisApp
  case book(ISBN, title: String, author: String)
  case bookReference(Book)
}

By bundling up the product of ISBN and the title/author strings into an identifiable struct, this would become identifiable via synthesis:

extension Book {
  struct Value: Identifiable {
    let id: ISBN
    var title: String
    var author: String
  }
}

enum MyTableCell: Identifiable {
  case aboutThisApp
  case book(Book.Value)
  case bookReference(Book)
}

This is exactly what SwiftUI would demand of you if you wanted to use .sheet(item:) with the product of (ISBN, String, String).

3 Likes