Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift - 'AnyObject!' is not convertible to 'ViewController'; did you mean to use 'as!' to force downcast?

Tags:

ios

swift

xcode6

I just updated my xcode from 6.2 to 6.3.1. The problem is I got a lot of this error message inside my project.

/Users/MNurdin/Documents/iOS/xxxxx/Controllers/Profile/DirectoryTableViewController.swift:31:98: 'AnyObject!' is not convertible to 'ViewController'; did you mean to use 'as!' to force downcast?

One of my code that affected with this error message.

let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
            let viewController = storyBoard.instantiateViewControllerWithIdentifier("LoginView") as! ViewController
            self.presentViewController(viewController, animated:true, completion:nil)
like image 730
Nurdin Avatar asked Dec 20 '22 04:12

Nurdin


1 Answers

After updating to Xcode 6.3, you are now using Swift 1.2

Prior to Swift 1.2, as was also used for forced conversion.

Swift 1.2 now represents forced conversions with as! to make it clear that the conversion may fail if you attempt to downcast to a type that doesn’t actually represent the value’s type.

So you have to use as! instead of as

like image 84
saurabh Avatar answered Dec 21 '22 23:12

saurabh