简体   繁体   English

来自 UIViewController 的条件转换总是成功 | 斯威夫特/Xcode

[英]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.

相关问题 从“ BaseViewController”到“ FUIAuthDelegate”的条件转换始终成功 - Conditional cast from 'BaseViewController' to 'FUIAuthDelegate' always succeeds 从“UIButton”到“UIButton”的条件转换总是成功 - Conditional cast from 'UIButton' to 'UIButton' always succeeds 黄色警告:从UITextDocumentProxy到UIKeyInput的条件强制转换始终成功 - yellow Warnings : Conditional cast from UITextDocumentProxy to UIKeyInput always succeeds 从“AppDelegate”到“UNUserNotificationCenterDelegate”的条件转换总是成功(颤振) - Conditional cast from 'AppDelegate' to 'UNUserNotificationCenterDelegate' always succeeds (Flutter) 如何删除警告“从'Any'到'AnyObject'的有条件强制转换始终成功” - How to remove warning “Conditional cast from 'Any' to 'AnyObject' always succeeds” 从字符串到字符串的有条件强制转换总是迅速完成 - Conditional cast from string to string always succeds in swift 3 从PFFile到PFFile的条件转换始终成功 - Conditional cast from PFFile toPFFile always succeed 从UITableViewCell到UITableViewCell的条件转换始终成功 - Conditional cast from UITableViewCell to UITableViewCell always succeds 从“ UIViewController”强制转换为无关类型“ TableViewCell”总是失败 - Cast from 'UIViewController' to unrelated type 'TableViewCell' always fails 无法从Spritekit SKScene转到另一个UIViewController,XCODE8 / Swift - Unable to segue from Spritekit SKScene to another UIViewController, XCODE8/Swift
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM