在一个现有的应用程序中,我想尝试SwiftUI的一些小视图。我在macOS Catalina和Xcode 11中打开了我的项目,并添加了SwiftUI类。
令我惊讶的是,我遇到了以下错误:
无法在此文件中预览-YourApp.app可能已崩溃
在预览窗口的顶部。
当我查看崩溃日志时,我发现这是由于stackoverflow错误:
崩溃的线程:0调度队列:com.apple.main-thread
Exception Type: EXC_BAD_ACCESS (SIGSEGV)
Exception Codes: KERN_PROTECTION_FAILURE at 0x00007ffee98cfff8
Exception Note: EXC_CORPSE_NOTIFY
Termination Signal: Segmentation fault: 11
Termination Reason: Namespace SIGNAL, Code 0xb
Terminating Process: exc handler [37403]
External Modification Warnings:
Thread creation by external task.
VM Regions Near 0x7ffee98cfff8:
MALLOC_SMALL 00007fa424000000-00007fa424800000 [ 8192K] rw-/rwx SM=PRV
--> STACK GUARD 00007ffee60d0000-00007ffee98d0000 [ 56.0M] ---/rwx SM=NUL stack guard for thread 0
Stack 00007ffee98d0000-00007ffeea0d0000 [ 8192K] rw-/rwx SM=ALI thread 0
Application Specific Information:
CoreSimulator 643.8.4 - Device: iPhone Xʀ (1C2CE9A2-334D-4A62-8AE9-C6CCC6D346E6) - Runtime: iOS 13.0 (17A5492t) - DeviceType: iPhone Xʀ
Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
0 MessageView.2.preview-thunk.dylib 0x000000012f726918 -[UIViewController(PageViewLogging) ms_viewWillAppear:] + 34
1 MessageView.2.preview-thunk.dylib 0x000000012f72691b -[UIViewController(PageViewLogging) ms_viewWillAppear:] + 37
2 MessageView.2.preview-thunk.dylib 0x000000012f72691b -[UIViewController(PageViewLogging) ms_viewWillAppear:] + 37
3 MessageView.2.preview-thunk.dylib 0x000000012f72691b -[UIViewController(PageViewLogging) ms_viewWillAppear:] + 37
...
508 MessageView.2.preview-thunk.dylib 0x000000012f72691b -[UIViewController(PageViewLogging) ms_viewWillAppear:] + 37
509 MessageView.2.preview-thunk.dylib 0x000000012f72691b -[UIViewController(PageViewLogging) ms_viewWillAppear:] + 37
510 MessageView.2.preview-thunk.dylib 0x000000012f72691b -[UIViewController(PageViewLogging) ms_viewWillAppear:] + 37
511 MessageView.2.preview-thunk.dylib 0x000000012f72691b -[UIViewController(PageViewLogging) ms_viewWillAppear:] + 37
答案 0 :(得分:0)
这是因为您在didFinishLaunchingWithOptions
中的AppDelegate
中使用了外部框架。在处理SwiftUI代码时清除该方法,它将按预期工作。
不确定为什么会这样,但这可能是因为SwiftUI运行您的应用程序实例来呈现它,并使用AppDelegate
来实现。