-2

I tried this solution but I get an error AnyObject is not convertible to 'UIViewController' did you mean to use 'as!' to force downcast?

What am I doing wrong?

I call the following code in LoginViewController viewDidLoad, I check if the user has logged in yet if yes then I want to skip the login view and go directly to Latest Photos which is FirstViewController...

override func viewDidLoad() {
    super.viewDidLoad()

    if(defaults.objectForKey("loggedIn") != nil){
        let vc : UIViewController = self.storyboard?.instantiateViewControllerWithIdentifier("LatestPhotosView") as! UIViewController;
        self.presentViewController(vc, animated: true, completion: nil)
    }

}
Community
  • 1
  • 1
Tony Morello
  • 462
  • 2
  • 19
  • It's ok to add links to external resources, but it would be much better if you include in the question the code you're having problems with. – Antonio Apr 17 '15 at 15:01

1 Answers1

5

Since Swift 1.2, you have to cast explicitly, change your as to as!.

This indicates to the programmer that the cast can fail. This decreases the probability of errors in your code.

Edit:

You have to call this in your viewDidAppear: method, as this will fail on your viewDidLoad() (Your view doesn't exist yet)

vrwim
  • 13,020
  • 13
  • 63
  • 118
  • Ok, now i get this at runtime **Warning: Attempt to present on whose view is not in the window hierarchy!** – Tony Morello Apr 17 '15 at 15:05
  • @TonyMorello I'll need some more code for that, edit your question to include more please. Where exactly do you call these methods? – vrwim Apr 17 '15 at 15:07
  • @wrwim ok... If I have to call it on view did appear I could just trigger the segue that already exists... That work flawlessly except I don't want the login screen to show at all and when view appears it shows for like half a second... is there a way to prevent it? – Tony Morello Apr 17 '15 at 15:21
  • If you want to skip a viewcontroller, you'll have to see where you load that view, and change the code there. If you want to skip the initial view controller, you'll have to load it manually from your `AppDelegate` like so: `window = UIWindow(frame: UIScreen.mainScreen().bounds); window.rootViewController = ...; window.makeKeyAndVisible()` – vrwim Apr 17 '15 at 15:26