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

标签 ios objective-c swift uistoryboard

您好,我正在尝试将此代码从 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/

相关文章:

ios - 检测 UIScrollView 是否在滚动

ios - 如何使 UITextField 只有一个文本/数字并使用 Next/Done 按钮浏览多个 `UITextField` s

ios - 带分页的UITableView,每个cell是一个UIScrollView

swift - 如何在 Swift2 中使用泛型类型

arrays - Swift 数组非常慢(在字典中使用时)

ios - SwiftUI 更改 EditButton 文本颜色

ios - 返回 null 的 NSData dataWithContentsOfFile

objective-c - NSPredicate 查找标题键以元音开头的所有对象

ios - 核心蓝牙 : scanForPeripheralsWithServices not working with service array

swift - 在打印时显示的 TextView 中可选