0

Can SwiftUI Text Fields work with optional Bindings?

import SwiftUI


struct CategoryListDetail: View {
  
    @Binding var firstCategory : Category?
    

    var body: some View {
        Form{
            Section(header: Text("Category")){
                TextField("Category Name", text: $firstCategory.name)
            }
            Toggle("Availability",isOn: $firstCategory.availability)
        }
    }
}

Cannot convert value of type 'Binding<String?>' to expected argument type 'Binding'

Here is the Category Entity

import Foundation
import CoreData


extension Category {

    @nonobjc public class func fetchRequest() -> NSFetchRequest<Category> {
        return NSFetchRequest<Category>(entityName: "Category")
    }

    @NSManaged public var id: UUID?
    @NSManaged public var name: String?
    @NSManaged public var availability: Bool
    @NSManaged public var sortOrder: Int64

}

extension Category : Identifiable {

}
PDev
  • 1
  • But then what happens when it _is_ nil? Force unwrap (potential crash)? Or default string? – George Sep 19 '21 at 03:56
  • Does this answer your question https://stackoverflow.com/a/59566387/12299030? – Asperi Sep 19 '21 at 05:03
  • Does this answer your question? [Cannot convert value of type 'Binding' to expected argument type 'Binding'](https://stackoverflow.com/questions/68543882/cannot-convert-value-of-type-bindingstring-to-expected-argument-type-bindi) – Phil Dukhov Sep 19 '21 at 11:25

0 Answers0