Adding the some code to one of my files in XCode causes the following error to show when trying to run previews for that file:
Compiling failed: replaced accessor for 'keyWindow' occurs in multiple places.
The error only happens when the following code is used in the file:
extension UIApplication {
var keyWindow: UIWindow? {
return UIApplication.shared.connectedScenes
.filter { $0.activationState == .foregroundActive }
.first(where: { $0 is UIWindowScene })
.flatMap({ $0 as? UIWindowScene })?.windows
.first(where: \.isKeyWindow)
}
var keyWindowPresentedController: UIViewController? {
var viewController = self.keyWindow?.rootViewController
if let presentedController = viewController as? UITabBarController {
viewController = presentedController.selectedViewController
}
while let presentedController = viewController?.presentedViewController {
if let presentedController = presentedController as? UITabBarController {
viewController = presentedController.selectedViewController
} else {
viewController = presentedController
}
}
return viewController
}
}
I know very little about UIKit, and this code was copy/pasted from online. Why is this code crashing my preview, and how can I fix it?Thanks!
I'm running XCode 13.4.1 on macOS Monterey
CodePudding user response:
UIApplication
already has keyWindow
property and it seems there is already other extension with same property in workspace, so just use different name for your, like
extension UIApplication {
var currentWindow: UIWindow? {
*or find which one another is and see if you can reuse it.