繁体   English   中英

此代码是否正确地从Objective-C转换为Swift?

[英]Is this code correctly translated from objective-c to swift?

嗨,我正在尝试将此代码从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;

很好的尝试。 但是,几乎没有什么变化可以迅速纠正您的代码。

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

获取故事板的方法如下:

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
}

我不明白您的问题,但是您的快速方法应该是这样

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!;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM