Emmanuel Amaro
Emmanuel Amaro

Reputation: 157

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

Hi i am trying to convert this code from Objective-c to Swift. Can anyone please confirm if this is correct or if anything needs changing.

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

This is what i have so far.

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)
    }


}

I also need help translating

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

and

    return storyboard;

Upvotes: 1

Views: 169

Answers (2)

VRAwesome
VRAwesome

Reputation: 4803

Good try. But little change should be there to correct your code in swift.

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

And method to get storyboard is like:

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
}

Upvotes: 1

Shoaib
Shoaib

Reputation: 2294

I did not understand your problem but your swift method should be like this

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

Upvotes: 0

Related Questions