gpt4 book ai didi

ios - 这段代码是否从 objective-c 正确地转换为 swift?

转载 作者:行者123 更新时间:2023-11-28 07:06:06 26 4
gpt4 key购买 nike

您好,我正在尝试将此代码从 Objective-c 转换为 Swift。任何人都可以确认这是否正确或是否需要更改。

- (UIStoryboard *)grabStoryboard {

UIStoryboard *storyboard;

// detect the height of our screen
int height = [UIScreen mainScreen].bounds.size.height;

if (height == 480) {
storyboard = [UIStoryboard storyboardWithName:@"Main3.5" bundle:nil];
} else if (height == 568) {
storyboard = [UIStoryboard storyboardWithName:@"Main4.0" bundle:nil];
}else {
storyboard = [UIStoryboard storyboardWithName:@"Main7.0" bundle:nil];
}

return storyboard;
}

这就是我目前所拥有的。

func grabStoryboard() {

var storyboard = UIStoryboard()

var height = UIScreen .mainScreen().bounds.size.height

if(height == 480){
storyboard = UIStoryboard(name: "3.5", bundle: nil)
} else if(height == 568){
storyboard = UIStoryboard(name: "4.0", bundle: nil)
}else{
storyboard = UIStoryboard(name: "7.0", bundle: nil)
}


}

我还需要翻译方面的帮助

UIStoryboard *storyboard = [self grabStoryboard];
self.window.rootViewController = [storyboard instantiateInitialViewController];
[self.window makeKeyAndVisible];

    return storyboard;

最佳答案

很好的尝试。但是应该几乎没有什么变化可以 swift 更正您的代码。

let storyboard:UIStoryboard = self.grabStoryboard()
self.window?.rootViewController = storyboard.instantiateInitialViewController() as? UIViewController
self.window?.makeKeyAndVisible()

获取 Storyboard的方法如下:

func grabStoryboard() -> UIStoryboard {

var storyboard = UIStoryboard()
var height = UIScreen .mainScreen().bounds.size.height

if(height == 480) {

storyboard = UIStoryboard(name: "3.5", bundle: nil)

} else if(height == 568) {

storyboard = UIStoryboard(name: "4.0", bundle: nil)

} else {

storyboard = UIStoryboard(name: "7.0", bundle: nil)
}

return storyboard
}

关于ios - 这段代码是否从 objective-c 正确地转换为 swift?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30587466/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com