[英]Conditional cast from UIViewController always succeeds | Swift/Xcode
What am I doing wrong here?我在这里做错了什么? This still functions fine, but I would like to get rid of the yellow warning if I can help it.这仍然可以正常工作,但如果可以的话,我想摆脱黄色警告。 The warning is on the "if" statement.警告在“if”语句中。 If I remove the "?"如果我删除“?” on "as", then the code won't even run... it requires it yet complains about it.在“as”上,然后代码甚至不会运行......它需要它但抱怨它。
The warning:警告:
Conditional cast from 'UIViewController' to 'UIViewController' always succeeds
The code:代码:
class FadeInPushSegue: UIStoryboardSegue {
var animated: Bool = true
override func perform() {
if let sourceViewController = self.source as? UIViewController, let destinationViewController = self.destination as? UIViewController {
let transition: CATransition = CATransition()
transition.type = CATransitionType.fade; sourceViewController.view.window?.layer.add(transition, forKey: "kCATransition")
sourceViewController.navigationController?.pushViewController(destinationViewController, animated: false)
}
}
}
You don't need to cast it to UIViewController because properties source and destinations is UIViewController already您不需要将其强制转换为 UIViewController,因为属性源和目标已经是 UIViewController
open var source: UIViewController { get }
open var destination: UIViewController { get }
You seeing this warning because you cast from not Optional UIViewController to optional UIViewController.您看到此警告是因为您从非可选 UIViewController 转换为可选 UIViewController。
When you remove as?
当你删除as?
your code isn't running because you trying to unwrap not optional property.您的代码未运行,因为您试图解包非可选属性。
Initializer for conditional binding must have Optional type, not 'UIViewController'
You should remove if
do something like that: if
做这样的事情,你应该删除:
final class FadeInPushSegue: UIStoryboardSegue {
var animated: Bool = true
override func perform() {
let sourceViewController = self.source
let destinationViewController = self.destination
let transition: CATransition = CATransition()
transition.type = CATransitionType.fade; sourceViewController.view.window?.layer.add(transition, forKey: "kCATransition")
sourceViewController.navigationController?.pushViewController(destinationViewController, animated: false)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.