微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

我的If let语句失败,我不确定为什么

如何解决我的If let语句失败,我不确定为什么

这是我的代码

override func prepare(for segue: UIStoryboardSegue,sender: Any?) {
    
    // Make sure we are acting on the correct segue
    if segue.identifier == "CreateJumpSpot",let jumpSpotCreatorControllerVC = segue.destination as? JumpSpotCreatorController {
        // Set the delegate in the JumpSpotCreatorController we're navigating to
        jumpSpotCreatorControllerVC.delegate = self
        
    } else if segue.identifier == "JumpSpotInfo",let jumpSpotInfoVC = segue.destination as? JumpSpotInfoController {
        print("eriubvwribvuorlaeD")
        if let senderAnnotationView = sender as? JumpSpotAnnotationView {
            let senderAnnotation = senderAnnotationView.annotation as? JumpSpotAnnotation
            jumpSpotInfoVC.titleLabel.text = senderAnnotation?.title
            jumpSpotInfoVC.imageView.image = senderAnnotation?.image
            jumpSpotInfoVC.descriptionLabel.text = senderAnnotation?.description
            jumpSpotInfoVC.heightLabel.text = senderAnnotation?.estimatedHeight
            jumpSpotInfoVC.warningsLabel.text = senderAnnotation?.warnings
            print("YOYOYOYO")
        }
    }
}

对于第二个标识符为“ JumpSpotInfo”的脚本,我知道代码正在运行if let语句,因为在调试器中显示了print(“ eriubvwribvuorlaeD”),但是我不知道为什么在代码中如果不执行。有什么想法吗?

解决方法

这是一个很好的例子,为什么强制展开会非常有用。

强制解开与segue直接相关的所有可选内容。如果代码崩溃,则您犯了一个设计错误,可以立即修复。如果所有类和标识符都设置正确,则代码不会崩溃。

通过可选绑定,什么也不会发生,而且你也不知道为什么。

并且您不能直接将值分配给标签。您需要在目标视图控制器中声明属性,并在viewDidLoad

中分配值
override func prepare(for segue: UIStoryboardSegue,sender: Any?) {
    
    // Make sure we are acting on the correct segue
    if segue.identifier == "CreateJumpSpot" {
       let jumpSpotCreatorControllerVC = segue.destination as! JumpSpotCreatorController
        // Set the delegate in the JumpSpotCreatorController we're navigating to
        jumpSpotCreatorControllerVC.delegate = self
        
    } else if segue.identifier == "JumpSpotInfo" {
        let jumpSpotInfoVC = segue.destination as! JumpSpotInfoController          
        print("eriubvwribvuorlaeD")
        let senderAnnotationView = sender as! JumpSpotAnnotationView
        let senderAnnotation = senderAnnotationView.annotation as! JumpSpotAnnotation
        // create the following properties in JumpSpotInfoController
        jumpSpotInfoVC.title = senderAnnotation.title
        jumpSpotInfoVC.image = senderAnnotation.image
        jumpSpotInfoVC.description = senderAnnotation.description
        jumpSpotInfoVC.height = senderAnnotation.estimatedHeight
        jumpSpotInfoVC.warnings = senderAnnotation.warnings
        print("YOYOYOYO")
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。