How does one declare a TableColumn with a nullable field?
I have a Book
model with several nullable fields:
struct Book: Codable, Equatable, Identifiable {
// ...
let productURL: String?
// ...
}
This is how I'm trying define the corresponding TableColumn
:
TableColumn("Product URL", value: \.productURL) { book in
Text(String(book.productURL ?? ""))
}
Though this results in several errors:
- Referencing initializer 'init(_:value:comparator:content:)' on 'TableColumn' requires that 'Book' inherit from 'NSObject'
- Referencing initializer 'init(_:value:comparator:content:)' on 'TableColumn' requires that 'Book' inherit from 'NSObject'
- Referencing initializer 'init(_:value:comparator:content:)' on 'TableColumn' requires the types 'KeyPathComparator' and 'SortDescriptor' be equivalent
- Referencing initializer 'init(_:value:comparator:content:)' on 'TableColumn' requires the types 'KeyPathComparator' and 'SortDescriptor' be equivalent
Other, non-nullable columns work just fine. For example:
TableColumn("ID", value: \.id) { book in
Text(String(book.id))
}
TableColumn("Slug", value: \.slug)
TableColumn("Category", value: \.category)
TableColumn("Title", value: \.title)
// ...
Did you just try to initialise it ? let productURL: String? = nil Note that I didn't test.
I already had an
init
constructor for the model. I tried initializing it where it's declared in the model,var productURL: String? = nil
, however, that didn't make a difference.One workaround would be to change my model to not have nullable fields, and set to "" when decoding a null. However, that to me, is a kludge. My model data is coming from a database table in Postgres where the column(s) are
NULLABLE
.