如何在SwiftUI中实现文本字段列表而不中断删除

时间:2019-12-10 15:24:26

标签: swiftui swiftui-list

当我

  1. 在XCode中创建一个主从应用程序
  2. 使用核心数据
  3. 向事件模型添加新字段(例如将title命名为String
  4. MasterView实现更改为此
struct MasterView: View {
    @FetchRequest(
        sortDescriptors: [NSSortDescriptor(keyPath: \Event.timestamp, ascending: true)], 
        animation: .default)
    var events: FetchedResults<Event>

    @Environment(\.managedObjectContext)
    var viewContext

    var body: some View {
        List {
            ForEach(events, id: \.self) { event in
                NavigationLink(
                    destination: DetailView(event: event)
                ) {
                    TextField("Title", text: Binding(ObservedObject<Event>(wrappedValue: event).projectedValue.title)!)
                }
            }.onDelete { indices in
                self.events.delete(at: indices, from: self.viewContext)
            }
        }
    }
}

,即,将Text换成TextField,并绑定到title对象的event属性的适当绑定,XCode会愉快地编译,并且应用程序将使用只要我不尝试从列表中删除,就会有预期的行为。

当我尝试删除Event时,此堆栈跟踪会崩溃

* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)
    #0: [33m`SwiftUI.BindingOperations.ForceUnwrapping.get(base: Swift.Optional<A>) -> A[0m
    #1: [33m`protocol witness for SwiftUI.Projection.get(base: A.Base) -> A.Projected in conformance SwiftUI.BindingOperations.ForceUnwrapping<A> : SwiftUI.Projection in SwiftUI[0m
    #2: [33m`SwiftUI.(ProjectedLocation in _5A9440699EF65619D724050F1A6941EE).update(context: AttributeGraph.AttributeContext<SwiftUI.VoidAttribute>, state: inout Swift.Optional<Any>) -> (B.Projected, Swift.Bool)[0m
    #3: [33m`SwiftUI.LocationBox.update(context: AttributeGraph.AttributeContext<SwiftUI.VoidAttribute>, state: inout Swift.Optional<Any>) -> (A.Value, Swift.Bool)[0m
    #4: [33m`SwiftUI.Binding.(ScopedLocation in _5436F2B399369BE3B016147A5F8FE9F2).update(context: AttributeGraph.AttributeContext<SwiftUI.VoidAttribute>, state: inout Swift.Optional<Any>) -> (A, Swift.Bool)[0m
    #5: [33m`protocol witness for SwiftUI.Location.update(context: AttributeGraph.AttributeContext<SwiftUI.VoidAttribute>, state: inout Swift.Optional<Any>) -> (A.Value, Swift.Bool) in conformance SwiftUI.Binding<A>.(ScopedLocation in _5436F2B399369BE3B016147A5F8FE9F2) : SwiftUI.Location in SwiftUI[0m
    #6: [33m`SwiftUI.LocationBox.update(context: AttributeGraph.AttributeContext<SwiftUI.VoidAttribute>, state: inout Swift.Optional<Any>) -> (A.Value, Swift.Bool)[0m
    #7: [33m`SwiftUI.Binding.(Box in _5436F2B399369BE3B016147A5F8FE9F2).update(property: inout SwiftUI.Binding<A>, context: AttributeGraph.AttributeContext<SwiftUI.VoidAttribute>) -> Swift.Bool[0m
    #8: [33m`static SwiftUI.(BoxVTable in _68550FF604D39F05971FE35A26EE75B0).update(ptr: Swift.UnsafeMutableRawPointer, property: Swift.UnsafeMutableRawPointer, context: AttributeGraph.AttributeContext<SwiftUI.VoidAttribute>) -> Swift.Bool[0m
    #9: [33m`SwiftUI._DynamicPropertyBuffer.update(container: Swift.UnsafeMutableRawPointer, context: AttributeGraph.AttributeContext<SwiftUI.VoidAttribute>) -> Swift.Bool[0m
    #10: [33m`SwiftUI.(DynamicPropertyBody in _9F92ACD17B554E8AB7D29ABB1E796415).update(context: inout AttributeGraph.AttributeContext<SwiftUI.(DynamicPropertyBody in _9F92ACD17B554E8AB7D29ABB1E796415)<A>>) -> ()[0m
    #11: [33m`protocol witness for static AttributeGraph.UntypedAttribute._update(_: Swift.UnsafeMutableRawPointer, graph: __C.AGGraphRef, attribute: __C.AGAttribute) -> () in conformance SwiftUI.(DynamicPropertyBody in _9F92ACD17B554E8AB7D29ABB1E796415)<A> : AttributeGraph.UntypedAttribute in SwiftUI[0m
    #12: [33m`partial apply forwarder[0m
    #13: [33m`AG::Graph::UpdateStack::update[0m
    #14: [33m`AG::Graph::update_attribute[0m
    #15: [33m`AG::Subgraph::update[0m
    #16: [33m`SwiftUI.ViewGraph.(runTransaction in _D63C4EB7F2B205694B6515509E76E98B)(in: __C.AGGraphRef) -> ()[0m
    #17: [33m`closure #1 (__C.AGGraphRef) -> (prefs: Swift.Bool, idealSize: Swift.Bool, outputs: SwiftUI.ViewGraph.Outputs) in SwiftUI.ViewGraph.updateOutputs(at: SwiftUI.Time) -> ()[0m
    #18: [33m`SwiftUI.ViewGraph.updateOutputs(at: SwiftUI.Time) -> ()[0m
    #19: [33m`closure #1 () -> () in closure #1 () -> () in (extension in SwiftUI):SwiftUI.ViewRendererHost.render(interval: Swift.Double, updateDisplayList: Swift.Bool) -> ()[0m
    #20: [33m`closure #1 () -> () in (extension in SwiftUI):SwiftUI.ViewRendererHost.render(interval: Swift.Double, updateDisplayList: Swift.Bool) -> ()[0m
    #21: [33m`(extension in SwiftUI):SwiftUI.ViewRendererHost.render(interval: Swift.Double, updateDisplayList: Swift.Bool) -> ()[0m
    #22: [33m`closure #1 () -> () in SwiftUI._UIHostingView.requestImmediateUpdate() -> ()[0m
    #23: [33m`reabstraction thunk helper from @escaping @callee_guaranteed () -> () to @escaping @callee_unowned @convention(block) () -> ()[0m
    #24: [33m`_dispatch_call_block_and_release[0m
    #25: [33m`_dispatch_client_callout[0m
    #26: [33m`_dispatch_main_queue_callback_4CF[0m
    #27: [33m`__CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__[0m
    #28: [33m`__CFRunLoopRun[0m
    #29: [33m`CFRunLoopRunSpecific[0m
    #30: [33m`GSEventRunModal[0m
    #31: [33m`UIApplicationMain[0m
  * #32: [33m`main[0m at AppDelegate.swift:13
    #33: [33m`start[0m

我已经尝试过:SwiftUI holding reference to deleted core data object causing crash,但无济于事。我还尝试过将文本字段提取到单独的视图类中,并使用适当的propertywrappers。

我想我以某种方式滥用了基础架构(绑定,可观察对象等)。我应该如何使用它们来实现列表中的文本字段?

2 个答案:

答案 0 :(得分:3)

代替

  

TextField(“ Title”,文本:Binding(ObservedObject(wrappedValue:   事件)。projectedValue.title)!)

使用

TextField("Title", text: Binding<String>(
                   get: {event.title ?? "<none>"}, set: {event.title = $0}))

在Xcode 11.2,iOS 13.2上进行了测试并可以使用

答案 1 :(得分:0)

一些想法:

选项1 (内联装订)

(我猜可能会有更简洁的实现,但是这段代码可以正常工作)

// Somewhere in MasterView...
// A function that returns a binding for the title of an event
func titleBindingFor(_ event: Event) -> Binding<String> {
    Binding<String>(get: { () -> String in
        event.title ?? ""
    }) { (title) in
        event.title = title
    }
}

然后

// Bind it to the textfield
TextField("Title", text: self.titleBindingFor(event))

选项2 (独立视图)

extension Optional where Wrapped == String {
    var safe: String {
        get { self ?? "" }
        set { self = newValue }
    }
}

struct TitleEditor: View {
    @ObservedObject var event: Event

    var body: some View {
        TextField("Title", text: $event.title.safe)
    }
}

然后

TitleEditor(event: event)

注意:我还必须处理DetailView中可选的时间戳力解包(真的是Apple吗?!),原因是删除时我崩溃了:

struct DetailView: View {
    @ObservedObject var event: Event

    var body: some View {
        let timestamp = event.timestamp ?? Date()
        return Text("\(timestamp, formatter: dateFormatter)")
            .navigationBarTitle(Text("Detail"))
    }
}

请注意,如果标题为nil,我将返回一个空字符串,该字符串由文本字段适当处理(它显示新创建的项目上的占位符)