请在Xcode的模拟器上模拟以下应用程序:
import UIKit
import CoreLocationUI
class ViewController: UIViewController {
let textField: UITextField = {
let tf = UITextField()
tf.backgroundColor = .systemGray6
tf.placeholder = "Tap me"
tf.leftView = UIView(frame: .init(x: 0, y: 0, width: 8, height: 0))
tf.leftViewMode = .always
tf.layer.cornerRadius = 11
return tf
}()
let locationButton = CLLocationButton()
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .systemBackground
view.addSubview(textField)
view.addSubview(locationButton)
textField.translatesAutoresizingMaskIntoConstraints = false
locationButton.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
textField.centerXAnchor.constraint(equalTo: view.centerXAnchor),
textField.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 8),
textField.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 0.85),
textField.heightAnchor.constraint(equalToConstant: 44),
locationButton.centerXAnchor.constraint(equalTo: view.centerXAnchor),
locationButton.topAnchor.constraint(equalTo: textField.bottomAnchor, constant: 8),
locationButton.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 0.85),
locationButton.heightAnchor.constraint(equalToConstant: 44),
])
}
}
如果我点击文本字段,应用程序就会崩溃。错误信息:
线程 1:“*** -[__NSDictionaryM setObject:forKey:]:对象不能为 nil(键:arrow.uturn.forward)”。
Xcode 15.3、MacBook Air M1 8GB、macOS Sonoma 14.4、模拟器操作系统:iOS 17.4。
我注意到它在我的 iPhone SE 第一代 32GB (iOS 15.8) 上不会崩溃。
不幸的是,我没有其他 iOS 设备可以尝试。
这只是模拟器的错误吗?
控制台日志:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSDictionaryM setObject:forKey:]: object cannot be nil (key: arrow.uturn.forward)'
*** First throw call stack:
(
0 CoreFoundation 0x00000001804ae138 __exceptionPreprocess + 172
1 libobjc.A.dylib 0x0000000180087db4 objc_exception_throw + 56
2 CoreFoundation 0x000000018051e88c -[__NSDictionaryM setObject:forKey:] + 1232
3 UIKitCore 0x00000001856c0f44 +[UIAssistantBarButtonItemProvider configuredSymbolImageWithName:size:] + 344
4 UIKitCore 0x00000001856c00bc +[UIAssistantBarButtonItemProvider barButtonItemForAssistantItemStyle:target:forcePlainButton:] + 2120
5 UIKitCore 0x00000001856c1200 +[UIAssistantBarButtonItemProvider defaultSystemLeadingBarButtonGroupsForItem:] + 204
6 UIKitCore 0x00000001856c1754 +[UIAssistantBarButtonItemProvider systemDefaultAssistantItem] + 48
7 UIKitCore 0x0000000185417e0c -[UIResponder(UIResponderInputViewAdditions) inputAssistantItem] + 64
8 UIKitCore 0x0000000185717710 -[UITextField inputAssistantItem] + 64
9 UIKitCore 0x0000000185417d4c _UIResponderFindInputAssistantItem + 44
10 UIKitCore 0x0000000184d79c18 -[UISystemInputAssistantViewController isVisibleWhenMinimized] + 96
11 UIKitCore 0x000000018523c12c +[UIPeripheralHost(UIKitInternal) endPlacementForInputViewSet:windowScene:] + 1292
12 UIKitCore 0x0000000184e0b2d0 -[UIKeyboardSceneDelegate prepareToMoveKeyboardForInputViewSet:animationStyle:] + 192
13 UIKitCore 0x0000000184e0a060 -[UIKeyboardSceneDelegate setKeyWindowSceneInputViews:animationStyle:] + 1396
14 UIKitCore 0x0000000184e09ab8 -[UIKeyboardSceneDelegate setInputViews:animationStyle:] + 132
15 UIKitCore 0x0000000184e0aaa4 -[UIKeyboardSceneDelegate setInputViews:animated:] + 72
16 UIKitCore 0x0000000184e0aaf4 -[UIKeyboardSceneDelegate setInputViews:] + 52
17 UIKitCore 0x0000000184e081f0 __102-[UIKeyboardSceneDelegate _reloadInputViewsForKeyWindowSceneResponder:force:fromBecomeFirstResponder:]_block_invoke.484 + 24
18 UIKitCore 0x00000001852386e0 __65-[UIPeripheralHost(UIKitInternal) queueDelayedTask:forKey:delay:]_block_invoke + 156
19 libdispatch.dylib 0x00000001016fd73c _dispatch_client_callout + 16
20 libdispatch.dylib 0x0000000101700c14 _dispatch_continuation_pop + 756
21 libdispatch.dylib 0x00000001017164e0 _dispatch_source_invoke + 1736
22 libdispatch.dylib 0x000000010170d2a8 _dispatch_main_queue_drain + 892
23 libdispatch.dylib 0x000000010170cf1c _dispatch_main_queue_callback_4CF + 40
24 CoreFoundation 0x000000018040e9a0 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 12
25 CoreFoundation 0x00000001804090b8 __CFRunLoopRun + 1936
26 CoreFoundation 0x0000000180408514 CFRunLoopRunSpecific + 572
27 GraphicsServices 0x000000018ef06ae4 GSEventRunModal + 160
28 UIKitCore 0x00000001853e8040 -[UIApplication _run] + 868
29 UIKitCore 0x00000001853ebcc8 UIApplicationMain + 124
30 UIKitCore 0x000000018488c1cc __swift_destroy_boxed_opaque_existential_1Tm + 10048
31 Crash_CLLocationButton 0x000000010057044c $sSo21UIApplicationDelegateP5UIKitE4mainyyFZ + 120
32 Crash_CLLocationButton 0x00000001005703c4 $s22Crash_CLLocationButton11AppDelegateC5$mainyyFZ + 44
33 Crash_CLLocationButton 0x00000001005704c8 main + 28
34 dyld 0x00000001005d1544 start_sim + 20
35 ??? 0x00000001006e60e0 0x0 + 4302201056
36 ??? 0x027c800000000000 0x0 + 179158822676332544
)
libc++abi: terminating due to uncaught exception of type NSException
这绝对是当应用程序在装有 iOS 17 的 iPad 或 iPad 模拟器上运行时出现的 iOS 错误。iOS 15 或 16 不会发生崩溃(至少在 iOS 15.5 或 16.4 中)。
错误提到了值
arrow.uturn.forward
。这是一个 SF 符号名称,通常用于 iPad 键盘工具栏中显示的“重做”按钮。
如果应用程序创建了
CLLocationButton
的实例(无论该按钮是否添加到视图层次结构中),都会出现该错误。在 iOS 17 下,应用程序崩溃,如问题所示。在 iOS 15 和 16 下,不会发生崩溃,但正常的撤消/重做/粘贴图标被“撤消”/“重做”/粘贴“替换。奇怪的是,在 iOS 16 下,粘贴按钮同时显示“粘贴”和粘贴图标相互重叠。
有一个解决方法。将以下两行添加到
UITextField
设置代码中:
tf.inputAssistantItem.leadingBarButtonGroups = []
tf.inputAssistantItem.trailingBarButtonGroups = []
这两行从 iPad 键盘工具栏中删除了按钮,从而防止了崩溃。删除这些工具栏项可能并不理想,但它比应用程序崩溃要好。
同时,使用反馈助手应用程序并使用您的代码提交完整的测试应用程序作为此错误的演示。也请随意包含此答案中的信息。