let scene = UIApplication.shared.connectedScenes.first
if let sd: SceneDelegate = (scene?.delegate as? SceneDelegate) {
let window = sd.window
}这是在Swift中,我想把它转换成Objective-C。我已经尝试过了:
UIScene *scene = [[[[UIApplication sharedApplication]connectedScenes]allObjects]firstObject];但是现在没有SceneDelegate;在Objective-C中有UIKit的UIWindowSceneDelegate。
此外,我无法访问UIScenedelegate。Scenedelegate在Swift中,我正在尝试在objective-c中获取window,但现在我无法访问swiftUI中的window。
发布于 2020-01-06 23:28:56
检查scene.delegate以确保它符合协议,然后在其上强制转换协议,以便编译器允许您使用协议属性和/或方法。
UIScene *scene = [[[[UIApplication sharedApplication] connectedScenes] allObjects] firstObject];
if([scene.delegate conformsToProtocol:@protocol(UIWindowSceneDelegate)]){
UIWindow *window = [(id <UIWindowSceneDelegate>)scene.delegate window];
}https://stackoverflow.com/questions/59613517
复制相似问题