SVProgressHUD在Xcode11以后的项目中无法运行

2020/10/23 posted in  BUG记录 总阅读量

错误提示

运行SVProgressHUD.show()后直接崩溃,报下面的错误

2020-10-23 11:12:48.021999+0800 wealth[9884:155319] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[wealth.AppDelegate window]: unrecognized selector sent to instance 0x6000007b8190'
*** First throw call stack:
(
    0   CoreFoundation                      0x00007fff2043a126 __exceptionPreprocess + 242
    1   libobjc.A.dylib                     0x00007fff20177f78 objc_exception_throw + 48
    2   CoreFoundation                      0x00007fff20448c6f +[NSObject(NSObject) instanceMethodSignatureForSelector:] + 0
    3   UIKitCore                           0x00007fff24695af8 -[UIResponder doesNotRecognizeSelector:] + 292
    4   CoreFoundation                      0x00007fff2043e3f0 ___forwarding___ + 859
    5   CoreFoundation                      0x00007fff20440698 _CF_forwarding_prep_0 + 120
    6   SVProgressHUD                       0x0000000100e51325 __27+[SVProgressHUD sharedView]_block_invoke + 117
    7   libdispatch.dylib                   0x00000001018b5a88 _dispatch_client_callout + 8
    8   libdispatch.dylib                   0x00000001018b6ff3 _dispatch_once_callout + 66
    9   SVProgressHUD                       0x0000000100e51288 +[SVProgressHUD sharedView] + 136
    10  SVProgressHUD                       0x0000000100e5267d +[SVProgressHUD showProgress:status:] + 61
    11  SVProgressHUD                       0x0000000100e52454 +[SVProgressHUD showWithStatus:] + 68
    12  SVProgressHUD                       0x0000000100e52365 +[SVProgressHUD show] + 37

原因

Xcode 11后增加了sence的概念,window可以是多个引起的。

解决办法

AppDelegate文件中添加如下代码

lazy var window: UIWindow? = {
    return UIApplication.shared.windows.first
}()