Swift - 'AnyObject!' 不能转换为 'ViewController';你是想用 'as!' 来强制向下转换吗?
Swift - 'AnyObject!' is not convertible to 'ViewController'; did you mean to use 'as!' to force downcast?
我刚刚将 xcode 从 6.2 更新到 6.3.1。问题是我的项目中有很多这样的错误消息。
/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?
受此错误消息影响的我的代码之一。
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let viewController = storyBoard.instantiateViewControllerWithIdentifier("LoginView") as! ViewController
self.presentViewController(viewController, animated:true, completion:nil)
更新到Xcode 6.3后,您现在使用的是Swift 1.2
在Swift1.2之前,as
也被用于强制转换。
Swift 1.2 现在用 as!
表示强制转换,以明确如果您尝试向下转换为实际上不表示值类型的类型,转换可能会失败。
所以你必须使用 as!
而不是 as
我刚刚将 xcode 从 6.2 更新到 6.3.1。问题是我的项目中有很多这样的错误消息。
/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?
受此错误消息影响的我的代码之一。
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let viewController = storyBoard.instantiateViewControllerWithIdentifier("LoginView") as! ViewController
self.presentViewController(viewController, animated:true, completion:nil)
更新到Xcode 6.3后,您现在使用的是Swift 1.2
在Swift1.2之前,as
也被用于强制转换。
Swift 1.2 现在用 as!
表示强制转换,以明确如果您尝试向下转换为实际上不表示值类型的类型,转换可能会失败。
所以你必须使用 as!
而不是 as