我是IOS开发的新手。我试图找到一个根视图控制器使用以下代码:
print("UIApplication.shared.keyWindow?.rootViewController : " , UIApplication.shared.keyWindow?.rootViewController)
我在两个视图中应用了这条线。一种观点认为它运行良好。但另一种观点是,它得到了如下错误:
Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArray0 objectAtIndex:]: index 0 beyond bounds for empty NSArray
有人知道如何解决这个错误吗?。提前谢谢。
对于swift 5及以上版本,请使用以下命令
print("UIApplication.shared.windows.first!.rootViewController : " , UIApplication.shared.windows.first!.rootViewController)