gpt4 book ai didi

swiftui - 如何在不中断删除的情况下在 SwiftUI 中实现 TextField 列表

转载 作者:行者123 更新时间:2023-12-04 20:26:41 33 4
gpt4 key购买 nike

当我

  • 在 XCode 中创建主从应用程序
  • 使用核心数据
  • 向事件模型添加一个新字段(例如 title 作为 String )
  • 更改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对于 TextFieldtitle 的适当绑定(bind) 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但无济于事。我还尝试将文本字段提取到单独的 View 类中并使用适当的属性包装器。

    我想我以某种方式滥用了基础设施(绑定(bind)、可观察对象等)。我应该如何使用这些来实现列表中的文本字段?

    最佳答案

    而不是

    TextField("Title", text: Binding(ObservedObject(wrappedValue: event).projectedValue.title)!)


    使用
    TextField("Title", text: Binding<String>(
    get: {event.title ?? "<none>"}, set: {event.title = $0}))
    经过测试并与 Xcode 11.2、iOS 13.2 一起使用

    关于swiftui - 如何在不中断删除的情况下在 SwiftUI 中实现 TextField 列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59270505/

    33 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com