简体   繁体   English

在 iOS 14 中编辑 TextField 时 SwiftUI 崩溃

[英]SwiftUI crash on editing TextField in iOS 14

I am facing a crash while editing my Custom textfield in iOS 14 and the crash log doesn't have enough information to know what is actually causing the crash.我在 iOS 14 中编辑我的自定义文本字段时遇到了崩溃,并且崩溃日志没有足够的信息来了解真正导致崩溃的原因。 I am showing 2 different Lists in the UI based on the editing mode of the textfield.我根据文本字段的编辑模式在 UI 中显示 2 个不同的列表。 I have attached a crash log below and also to note that this is working fine in iOS 15.我在下面附上了崩溃日志,还要注意这在 iOS 15 中运行良好。

0  libobjc.A.dylib                0x25c80 objc_retain + 16
1  SwiftUI                        0x4a37e0 initializeWithCopy for ViewRootBody + 32
2  SwiftUI                        0x292120 implicit closure #2 in implicit closure #1 in closure #1 in closure #1 in Attribute.init<A>(_:) + 236
3  AttributeGraph                 0x577c AG::Graph::UpdateStack::update() + 492
4  AttributeGraph                 0x5bb4 AG::Graph::update_attribute(AG::data::ptr<AG::Node>, bool) + 332
5  AttributeGraph                 0xb2fc AG::Graph::input_value_ref_slow(AG::data::ptr<AG::Node>, AG::AttributeID, unsigned int, AGSwiftMetadata const*, bool*, long) + 364
6  AttributeGraph                 0x1d09c AGGraphGetValue + 232
7  SwiftUI                        0x736090 DynamicBody.updateValue() + 276
8  SwiftUI                        0x3afb00 partial apply for implicit closure #2 in implicit closure #1 in closure #1 in closure #1 in Attribute.init<A>(_:) + 32
9  AttributeGraph                 0x577c AG::Graph::UpdateStack::update() + 492
10 AttributeGraph                 0x5bb4 AG::Graph::update_attribute(AG::data::ptr<AG::Node>, bool) + 332
11 AttributeGraph                 0xb2fc AG::Graph::input_value_ref_slow(AG::data::ptr<AG::Node>, AG::AttributeID, unsigned int, AGSwiftMetadata const*, bool*, long) + 364
12 AttributeGraph                 0x1d09c AGGraphGetValue + 232
13 SwiftUI                        0xb04b74 _ConditionalContent<>.ChildView.content.getter + 64
14 SwiftUI                        0xb04ddc _ConditionalContent<>.ChildView.value.getter + 524
15 SwiftUI                        0xb04f00 protocol witness for Rule.value.getter in conformance _ConditionalContent<A, B><>.ChildView + 44
16 SwiftUI                        0x292130 implicit closure #2 in implicit closure #1 in closure #1 in closure #1 in Attribute.init<A>(_:) + 252
17 AttributeGraph                 0x577c AG::Graph::UpdateStack::update() + 492
18 AttributeGraph                 0x5bb4 AG::Graph::update_attribute(AG::data::ptr<AG::Node>, bool) + 332
19 AttributeGraph                 0xb2fc AG::Graph::input_value_ref_slow(AG::data::ptr<AG::Node>, AG::AttributeID, unsigned int, AGSwiftMetadata const*, bool*, long) + 364
20 AttributeGraph                 0x1d09c AGGraphGetValue + 232
21 SwiftUI                        0x73bbf4 AnyViewChildList.view.getter + 28
22 SwiftUI                        0x73bce8 AnyViewChildList.updateValue() + 164
23 SwiftUI                        0x3afb00 partial apply for implicit closure #2 in implicit closure #1 in closure #1 in closure #1 in Attribute.init<A>(_:) + 32
24 AttributeGraph                 0x577c AG::Graph::UpdateStack::update() + 492
25 AttributeGraph                 0x5bb4 AG::Graph::update_attribute(AG::data::ptr<AG::Node>, bool) + 332
26 AttributeGraph                 0xb2fc AG::Graph::input_value_ref_slow(AG::data::ptr<AG::Node>, AG::AttributeID, unsigned int, AGSwiftMetadata const*, bool*, long) + 364
27 AttributeGraph                 0x1d09c AGGraphGetValue + 232
28 SwiftUI                        0x735694 StaticBody.container.getter + 80
29 SwiftUI                        0x735e00 StaticBody.updateValue() + 196
30 SwiftUI                        0x3afb00 partial apply for implicit closure #2 in implicit closure #1 in closure #1 in closure #1 in Attribute.init<A>(_:) + 32
31 AttributeGraph                 0x577c AG::Graph::UpdateStack::update() + 492
32 AttributeGraph                 0x5bb4 AG::Graph::update_attribute(AG::data::ptr<AG::Node>, bool) + 332
33 AttributeGraph                 0xb2fc AG::Graph::input_value_ref_slow(AG::data::ptr<AG::Node>, AG::AttributeID, unsigned int, AGSwiftMetadata const*, bool*, long) + 364
34 AttributeGraph                 0x1d09c AGGraphGetValue + 232
35 SwiftUI                        0x488c00 SecondaryLayerGeometryQuery.alignment.getter + 60
36 SwiftUI                        0x488dd0 SecondaryLayerGeometryQuery.value.getter + 156
37 SwiftUI                        0x1d1f3c specialized implicit closure #2 in implicit closure #1 in closure #1 in closure #1 in Attribute.init<A>(_:) + 36
38 AttributeGraph                 0x577c AG::Graph::UpdateStack::update() + 492
39 AttributeGraph                 0x5bb4 AG::Graph::update_attribute(AG::data::ptr<AG::Node>, bool) + 332
40 AttributeGraph                 0xb2fc AG::Graph::input_value_ref_slow(AG::data::ptr<AG::Node>, AG::AttributeID, unsigned int, AGSwiftMetadata const*, bool*, long) + 364
41 AttributeGraph                 0x1d09c AGGraphGetValue + 232
42 SwiftUI                        0x9e9238 AnimatableFrameAttribute.updateValue() + 52
43 SwiftUI                        0x250d90 partial apply for specialized implicit closure #2 in implicit closure #1 in closure #1 in closure #1 in Attribute.init<A>(_:) + 24
44 AttributeGraph                 0x577c AG::Graph::UpdateStack::update() + 492
45 AttributeGraph                 0x5bb4 AG::Graph::update_attribute(AG::data::ptr<AG::Node>, bool) + 332
46 AttributeGraph                 0xedc4 AG::Subgraph::update(unsigned int) + 884
47 SwiftUI                        0xa641c0 GraphHost.runTransaction() + 180
48 SwiftUI                        0xa67010 GraphHost.runTransaction(_:) + 92
49 SwiftUI                        0xa65880 GraphHost.flushTransactions() + 168
50 SwiftUI                        0xa66f60 closure #1 in closure #1 in GraphHost.asyncTransaction<A>(_:mutation:style:) + 24
51 SwiftUI                        0x4ed6b4 partial apply for closure #1 in ViewGraphDelegate.updateGraph<A>(body:) + 28
52 SwiftUI                        0x9afc8c closure #1 in ViewRendererHost.updateViewGraph<A>(body:) + 108
53 SwiftUI                        0x9a6840 ViewRendererHost.updateViewGraph<A>(body:) + 92
54 SwiftUI                        0x4e9a18 ViewGraphDelegate.updateGraph<A>(body:) + 80
55 SwiftUI                        0xa66f30 closure #1 in GraphHost.init(data:) + 160
56 SwiftUI                        0xa682bc partial apply for closure #1 in GraphHost.asyncTransaction<A>(_:mutation:style:) + 40
57 SwiftUI                        0x516008 thunk for @escaping @callee_guaranteed () -> () + 28
58 SwiftUI                        0x327374 static NSRunLoop.flushObservers() + 148
59 SwiftUI                        0x3272d8 closure #1 in closure #1 in static NSRunLoop.addObserver(_:) + 16
60 SwiftUI                        0x322250 specialized thunk for @callee_guaranteed () -> (@error @owned Error) + 24
61 libswiftObjectiveC.dylib       0x1f24 autoreleasepool<A>(invoking:) + 64
62 SwiftUI                        0x3272b8 closure #1 in static NSRunLoop.addObserver(_:) + 64
63 SwiftUI                        0x327410 @objc closure #1 in static NSRunLoop.addObserver(_:) + 56
64 CoreFoundation                 0xa2588 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 36
65 CoreFoundation                 0x9c6ac __CFRunLoopDoObservers + 572
66 CoreFoundation                 0x9cc58 __CFRunLoopRun + 1052
67 CoreFoundation                 0x9c308 CFRunLoopRunSpecific + 600
68 GraphicsServices               0x3734 GSEventRunModal + 164
69 UIKitCore                      0xbca75c -[UIApplication _run] + 1072
70 UIKitCore                      0xbcffcc UIApplicationMain + 168
71     0x52e28 main + 23 (__hidden#4115_:23)
72 libdyld.dylib                  0x1cf8 start + 4

Please find below my Custom textfield请在我的自定义文本字段下方找到

struct ClearableTextField: View {
    @State private var fieldState: FieldState = .normal
    let label: String
    let symbol: String?
    @Binding var text: String
    let validationRegex: String?
    let inputRegex: String?
    let errorText: String?
    var onEditingChanged: ((Bool) -> Void)?

    var body: some View {
        VStack(alignment: .leading) {
            TextField(label, text: $text, onEditingChanged: { edit in
                onEditingChanged?(edit)
                fieldState = edit ? .focused : .normal
                if !edit {
                    guard let validationRegex = validationRegex,
                          !text.matches(validationRegex) else { return }
                    fieldState = .error
                }
            })
            .onReceive(Just(text)) { newValue in
                guard let inputRegex = inputRegex, let value = newValue.last else { return }
                if String(value).matches(inputRegex) {
                    self.text = newValue
                } else {
                    self.text.removeLast()
                }
            }
//            .textFieldStyle(ClearableTextFieldStyle(text: $text,
//                                                    fieldState: $fieldState,
//                                                    symbol: symbol))
            if fieldState == .error, let errorText = errorText {
                Text(errorText)
                    .foregroundColor(fieldState.color)
                    .font(.caption)
                    .padding(.bottom, 12)
            }
        }
    }

    init(label: String,
         symbol: String? = nil,
         text: Binding<String>,
         validationRegex: String? = nil,
         inputRegex: String? = nil,
         errorText: String? = nil,
         onEditingChanged: ((Bool) -> Void)? = nil) {
        self.label = label
        self.symbol = symbol
        _text = text
        self.validationRegex = validationRegex
        self.inputRegex = inputRegex
        self.errorText = errorText
        self.onEditingChanged = onEditingChanged
    }
}  

Your code in onReceive closure additionally updates binding which may cause the crash.您在onReceive闭包中的代码还会更新可能导致崩溃的绑定。 Try this:尝试这个:

var body: some View {
    VStack(alignment: .leading) {
        TextField(label, 
                  text: .init(get: { self.text } 
                              set: {
                      guard let inputRegex = inputRegex, 
                            let value = $0.last,
                            !String(value).matches(inputRegex) else {
                          self.text = $0
                          return
                      }
                      self.text = $0.removeLast()
                  }, 
                  onEditingChanged: { edit in
                      onEditingChanged?(edit)
                      fieldState = edit ? .focused : .normal
                      if !edit {
                          guard let validationRegex = validationRegex,
                                !text.matches(validationRegex) else { return }
                          fieldState = .error
                      }
        })
//            .textFieldStyle(ClearableTextFieldStyle(text: $text,
//                                                    fieldState: $fieldState,
//                                                    symbol: symbol))

        if fieldState == .error, let errorText = errorText {
            Text(errorText)
                .foregroundColor(fieldState.color)
                .font(.caption)
                .padding(.bottom, 12)
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM