当 运行 来自设备而不是来自 Build 和 运行 时应用程序崩溃

App crashing when run from device but not from Build and Run

我对 Swift 和使用 Metal 还很陌生,所以如果我有很大的疏忽,请原谅。 我正在关注一个从 MetalKit 开始的教程,并且该应用程序在连接设备的情况下使用 "Build and Run" 运行良好,但是从 springboard 启动时它会在启动时立即崩溃。

在启动前附加调试器,我得到以下断点和 [UIResponder doesNotRecognizeSelector:]: 消息。这会在 Storyboard 初始化期间立即发生。

视图层次结构是 MySceneViewController: MetalViewController,MetalViewControllerDelegate 作为根视图控制器,在主故事板中设置下面的 mtkView。应用程序中目前没有其他内容。

同样,当 运行 直接来自 Xcode 时,这个完全相同的代码有效,所以我不知道我在这里做错了什么。

感谢您的帮助。如果需要,我很乐意添加任何其他信息。

编辑:添加了来自 phone 日志的调试跟踪

Incident Identifier: F48966B5-12B6-446B-8A78-86787F776564
CrashReporter Key:   308bc148d5da7a16581206abaf74f6b2a5776702
Hardware Model:      iPhone12,3
Process:             Test Project [6466]
Path:                /private/var/containers/Bundle/Application/EDB79F8D-B974-490E-8340-DD79F37CAB8C/Test Project.app/Test Project
Identifier:          com.MyName.TestProject
Version:             1 (1.0)
Code Type:           ARM-64 (Native)
Role:                Foreground
Parent Process:      launchd [1]
Coalition:           com.MyName.TestProject [2592]


Date/Time:           2019-11-25 23:05:43.1609 -0500
Launch Time:         2019-11-25 23:05:43.0752 -0500
OS Version:          iPhone OS 13.2.3 (17B111)
Release Type:        User
Baseband Version:    1.02.18
Report Version:      104

Exception Type:  EXC_CRASH (SIGABRT)
Exception Codes: 0x0000000000000000, 0x0000000000000000
Exception Note:  EXC_CORPSE_NOTIFY
Triggered by Thread:  0

Last Exception Backtrace:
0   CoreFoundation                  0x1862ffab0 __exceptionPreprocess + 224
1   libobjc.A.dylib                 0x186019028 objc_exception_throw + 59
2   CoreFoundation                  0x1861fdf10 -[NSObject+ 200464 (NSObject) doesNotRecognizeSelector:] + 143
3   UIKitCore                       0x18a414a58 -[UIResponder doesNotRecognizeSelector:] + 303
4   CoreFoundation                  0x186304254 ___forwarding___ + 1327
5   CoreFoundation                  0x1863063f0 _CF_forwarding_prep_0 + 95
6   Test Project                    0x104914f90 0x10490c000 + 36752
7   Test Project                    0x104914cc4 0x10490c000 + 36036
8   Test Project                    0x104914b78 0x10490c000 + 35704
9   Foundation                      0x1866a5b40 _NSSetUsingKeyValueSetter + 43
10  Foundation                      0x1865e5e40 -[NSObject+ 212544 (NSKeyValueCoding) setValue:forKey:] + 315
11  UIKitCore                       0x189d9a538 -[UIViewController setValue:forKey:] + 103
12  UIKitCore                       0x18a073124 -[UIRuntimeOutletConnection connect] + 127
13  libobjc.A.dylib                 0x186014c64 -[NSObject performSelector:] + 55
14  CoreFoundation                  0x1861d34dc -[NSArray makeObjectsPerformSelector:] + 255
15  UIKitCore                       0x18a070118 -[UINib instantiateWithOwner:options:] + 1971
16  UIKitCore                       0x189da1538 -[UIViewController _loadViewFromNibNamed:bundle:] + 367
17  UIKitCore                       0x189da1ffc -[UIViewController loadView] + 179
18  UIKitCore                       0x189da22cc -[UIViewController loadViewIfRequired] + 187
19  UIKitCore                       0x189da29b4 -[UIViewController view] + 31
20  UIKitCore                       0x18a41dc58 -[UIWindow addRootViewControllerViewIfPossible] + 175
21  UIKitCore                       0x18a41d354 -[UIWindow _updateLayerOrderingAndSetLayerHidden:actionBlock:] + 227
22  UIKitCore                       0x18a41e2f0 -[UIWindow _setHidden:forced:] + 367
23  UIKitCore                       0x18a42f4d4 -[UIWindow _mainQueue_makeKeyAndVisible] + 51
24  UIKitCore                       0x18a3dd2f0 -[UIApplication _callInitializationDelegatesWithActions:forCanvas:payload:fromOriginatingProcess:] + 3219
25  UIKitCore                       0x18a3e342c -[UIApplication _runWithMainScene:transitionContext:completion:] + 1311
26  UIKitCore                       0x189b7d55c -[_UISceneLifecycleMultiplexer completeApplicationLaunchWithFBSScene:transitionContext:] + 151
27  UIKitCore                       0x18a02ddb0 _UIScenePerformActionsWithLifecycleActionMask + 111
28  UIKitCore                       0x189b7e094 __101-[_UISceneLifecycleMultiplexer _evalTransitionToSettings:fromSettings:forceExit:withTransitionStore:]_block_invoke + 211
29  UIKitCore                       0x189b7dac4 -[_UISceneLifecycleMultiplexer _performBlock:withApplicationOfDeactivationReasons:fromReasons:] + 303
30  UIKitCore                       0x189b7deb0 -[_UISceneLifecycleMultiplexer _evalTransitionToSettings:fromSettings:forceExit:withTransitionStore:] + 751
31  UIKitCore                       0x189b7d734 -[_UISceneLifecycleMultiplexer uiScene:transitionedFromState:withTransitionContext:] + 339
32  UIKitCore                       0x189b81ee4 __186-[_UIWindowSceneFBSSceneTransitionContextDrivenLifecycleSettingsDiffAction _performActionsForUIScene:withUpdatedFBSScene:settingsDiff:fromSettings:transitionContext:lifecycleActionType:]_block_invoke_2 + 195
33  UIKitCore                       0x18a047c34 ___UISceneSettingsDiffActionPerformChangesWithTransitionContext_block_invoke + 27
34  UIKitCore                       0x189f5aeec +[BSAnimationSettings+ 5996268 (UIKit) tryAnimatingWithSettings:actions:completion:] + 867
35  UIKitCore                       0x18a047bec _UISceneSettingsDiffActionPerformChangesWithTransitionContext + 259
36  UIKitCore                       0x189b81bfc __186-[_UIWindowSceneFBSSceneTransitionContextDrivenLifecycleSettingsDiffAction _performActionsForUIScene:withUpdatedFBSScene:settingsDiff:fromSettings:transitionContext:lifecycleActionType:]_block_invoke + 151
37  UIKitCore                       0x18a047ad4 _UISceneSettingsDiffActionPerformActionsWithDelayForTransitionContext + 107
38  UIKitCore                       0x189b81a58 -[_UIWindowSceneFBSSceneTransitionContextDrivenLifecycleSettingsDiffAction _performActionsForUIScene:withUpdatedFBSScene:settingsDiff:fromSettings:transitionContext:lifecycleActionType:] + 391
39  UIKitCore                       0x1899e9b7c __64-[UIScene scene:didUpdateWithDiff:transitionContext:completion:]_block_invoke + 639
40  UIKitCore                       0x1899e8640 -[UIScene _emitSceneSettingsUpdateResponseForCompletion:afterSceneUpdateWork:] + 255
41  UIKitCore                       0x1899e98ac -[UIScene scene:didUpdateWithDiff:transitionContext:completion:] + 235
42  UIKitCore                       0x18a3e17e0 -[UIApplication workspace:didCreateScene:withTransitionContext:completion:] + 563
43  UIKitCore                       0x189f7cdec -[UIApplicationSceneClientAgent scene:didInitializeWithEvent:completion:] + 375
44  FrontBoardServices              0x18b4c9ec0 -[FBSSceneImpl _callOutQueue_agent_didCreateWithTransitionContext:completion:] + 451
45  FrontBoardServices              0x18b4f0b50 __86-[FBSWorkspaceScenesClient sceneID:createWithParameters:transitionContext:completion:]_block_invoke.168 + 115
46  FrontBoardServices              0x18b4d4fa4 -[FBSWorkspace _calloutQueue_executeCalloutFromSource:withBlock:] + 239
47  FrontBoardServices              0x18b4f07e4 __86-[FBSWorkspaceScenesClient sceneID:createWithParameters:transitionContext:completion:]_block_invoke + 343
48  libdispatch.dylib               0x185fa5fd8 _dispatch_client_callout + 19
49  libdispatch.dylib               0x185fa8d1c _dispatch_block_invoke_direct + 263
50  FrontBoardServices              0x18b517304 __FBSSERIALQUEUE_IS_CALLING_OUT_TO_A_BLOCK__ + 47
51  FrontBoardServices              0x18b516fb0 -[FBSSerialQueue _queue_performNextIfPossible] + 431
52  FrontBoardServices              0x18b51751c -[FBSSerialQueue _performNextFromRunLoopSource] + 31
53  CoreFoundation                  0x18627b24c __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 27
54  CoreFoundation                  0x18627b1a0 __CFRunLoopDoSource0 + 83
55  CoreFoundation                  0x18627a90c __CFRunLoopDoSources0 + 183
56  CoreFoundation                  0x1862757d8 __CFRunLoopRun + 1067
57  CoreFoundation                  0x186275084 CFRunLoopRunSpecific + 479
58  GraphicsServices                0x1904c3534 GSEventRunModal + 107
59  UIKitCore                       0x18a3e5670 UIApplicationMain + 1939
60  Test Project                    0x104926bf8 0x10490c000 + 109560
61  libdyld.dylib                   0x1860f4e18 start + 3


Thread 0 name:  Dispatch queue: com.apple.main-thread
Thread 0 Crashed:
0   libsystem_kernel.dylib          0x00000001860eaefc __pthread_kill + 8
1   libsystem_pthread.dylib         0x000000018600ad10 pthread_kill + 196
2   libsystem_c.dylib               0x0000000185f9aa74 abort + 104
3   libc++abi.dylib                 0x00000001860b23c8 __cxa_bad_cast + 0
4   libc++abi.dylib                 0x00000001860b25c0 demangling_unexpected_handler+ 5568 () + 0
5   libobjc.A.dylib                 0x0000000186019308 _objc_terminate+ 25352 () + 124
6   libc++abi.dylib                 0x00000001860bf634 std::__terminate(void (*)+ 58932 ()) + 20
7   libc++abi.dylib                 0x00000001860bf5c0 std::terminate+ 58816 () + 44
8   libdispatch.dylib               0x0000000185fa5fec _dispatch_client_callout + 40
9   libdispatch.dylib               0x0000000185fa8d1c _dispatch_block_invoke_direct + 264
10  FrontBoardServices              0x000000018b517304 __FBSSERIALQUEUE_IS_CALLING_OUT_TO_A_BLOCK__ + 48
11  FrontBoardServices              0x000000018b516fb0 -[FBSSerialQueue _queue_performNextIfPossible] + 432
12  FrontBoardServices              0x000000018b51751c -[FBSSerialQueue _performNextFromRunLoopSource] + 32
13  CoreFoundation                  0x000000018627b24c __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 28
14  CoreFoundation                  0x000000018627b1a0 __CFRunLoopDoSource0 + 84
15  CoreFoundation                  0x000000018627a90c __CFRunLoopDoSources0 + 184
16  CoreFoundation                  0x00000001862757d8 __CFRunLoopRun + 1068
17  CoreFoundation                  0x0000000186275084 CFRunLoopRunSpecific + 480
18  GraphicsServices                0x00000001904c3534 GSEventRunModal + 108
19  UIKitCore                       0x000000018a3e5670 UIApplicationMain + 1940
20  Test Project                    0x0000000104926bf8 0x10490c000 + 109560
21  libdyld.dylib                   0x00000001860f4e18 start + 4

Thread 1:
0   libsystem_pthread.dylib         0x0000000186011c74 start_wqthread + 0

Thread 2:
0   libsystem_pthread.dylib         0x0000000186011c74 start_wqthread + 0

Thread 3:
0   libsystem_pthread.dylib         0x0000000186011c74 start_wqthread + 0

Thread 4:
0   libsystem_pthread.dylib         0x0000000186011c74 start_wqthread + 0

Thread 5:
0   libsystem_pthread.dylib         0x0000000186011c74 start_wqthread + 0

Thread 6 name:  com.apple.uikit.eventfetch-thread
Thread 6:
0   libsystem_kernel.dylib          0x00000001860c8c04 mach_msg_trap + 8
1   libsystem_kernel.dylib          0x00000001860c8020 mach_msg + 76
2   CoreFoundation                  0x000000018627aaa8 __CFRunLoopServiceMachPort + 220
3   CoreFoundation                  0x0000000186275940 __CFRunLoopRun + 1428
4   CoreFoundation                  0x0000000186275084 CFRunLoopRunSpecific + 480
5   Foundation                      0x00000001865ba3d0 -[NSRunLoop+ 33744 (NSRunLoop) runMode:beforeDate:] + 232
6   Foundation                      0x00000001865ba2a8 -[NSRunLoop+ 33448 (NSRunLoop) runUntilDate:] + 92
7   UIKitCore                       0x000000018a481148 -[UIEventFetcher threadMain] + 156
8   Foundation                      0x00000001865b9034 -[NSThread main] + 40
9   Foundation                      0x00000001866f4a8c __NSThread__start__ + 852
10  libsystem_pthread.dylib         0x0000000186009d50 _pthread_start + 128
11  libsystem_pthread.dylib         0x0000000186011c88 thread_start + 8

Thread 0 crashed with ARM Thread State (64-bit):
    x0: 0x0000000000000000   x1: 0x0000000000000000   x2: 0x0000000000000000   x3: 0x0000000000000000
    x4: 0x000000016b4f1f30   x5: 0x000000016b4f24e0   x6: 0x000000000000006e   x7: 0x0000000000000600
    x8: 0x0000000104d3d840   x9: 0x00000000ce9b2cb7  x10: 0x0000000000000002  x11: 0x0000000000000003
   x12: 0x0000000000000000  x13: 0x000000000000002e  x14: 0x0000000000000010  x15: 0x0000000000000000
   x16: 0x0000000000000148  x17: 0x00000001c3b86f10  x18: 0x0000000000000000  x19: 0x0000000000000006
   x20: 0x0000000000000407  x21: 0x0000000104d3d920  x22: 0x0000000000000000  x23: 0x0000000104d3d920
   x24: 0x0000000000001c03  x25: 0x0000000000000001  x26: 0x000000016b4f2ca0  x27: 0x0000000000000000
   x28: 0x000000016b4f3b10   fp: 0x000000016b4f2440   lr: 0x000000018600ad10
    sp: 0x000000016b4f2420   pc: 0x00000001860eaefc cpsr: 0x40000000
   esr: 0x56000080  Address size fault

如果应用程序是从 Springboard 启动的,则 mtkView 的类型 (UIView) 错误。要检查它,您可以在调试器中使用命令 po

po self.mtkView
▿ Optional<MTKView>
  - some : <UIView: 0x10a907450; frame = (0 0; 375 667); autoresize = W+H; layer = <CALayer: 0x2837a65a0>>

只需将 Metall 和 MetalKit 框架添加到 linking 框架列表中:

我不知道为什么XCode没有link基于导入的框架,但它解决了问题。