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