performSegueWithIdentifier - 无法将'AnyObject'类型的值转换为参数类型'AnyObject?'

时间:2021-04-15 15:21:28

I am trying to pass some string data to a viewcontroller using performSegueWithIdentifier, but I get this error Cannot convert value of type 'AnyObject?'. Type(Aka'Optional<AnyObject>.Type) to expected argument type 'AnyObject?' Even if I use sender:self, it still does not work. In the storyboard, the segue is made by dragging a segue from 1st to 2nd view controller.

我试图使用performSegueWithIdentifier将一些字符串数据传递给viewcontroller,但我得到此错误无法转换类型'AnyObject?'的值。输入(Aka'Optional .Type)到预期的参数类型'AnyObject?'即使我使用发件人:self,它仍然无效。在故事板中,通过将segue从第1个视图控制器拖动到第2个视图控制器来制作segue。

@IBAction func resetPassword(sender: AnyObject) {



    FIRAuth.auth()?.sendPasswordResetWithEmail(emailTextField.text!, completion: { (error) in

        var customError = error?.localizedDescription

            if error == nil {

                let noError = "Click on the link received in the email"
                self.emailTextField.text = ""
                self.emailTextField.attributedPlaceholder = NSAttributedString(string: noError, attributes:[NSForegroundColorAttributeName: UIColor.blueColor()])
                self.customErroSent = noError

            performSegueWithIdentifier("fromSeventhToFifth", sender: AnyObject?)

                //self.resetButtonOutlet.hidden = true
              //  self.emailTextField.hidden = true

            } else {


                 self.emailTextField.text = ""
                self.emailTextField.attributedPlaceholder = NSAttributedString(string:customError!, attributes:[NSForegroundColorAttributeName: UIColor.redColor()])
            }
        })
    }

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "fromSeventhToFifth" {
        if let destViewController = segue.destinationViewController as? FifthViewController {
                    destViewController.label.text = customErroSent


                }
            }
        }
    }

3 个解决方案

#1


1  

The sender parameter is of type AnyObject? - so you can supply any object reference or nil, but you can't put AnyObject? since that is a type, not an object.

sender参数的类型是AnyObject吗? - 所以你可以提供任何对象引用或nil,但是你不能把AnyObject?因为那是一种类型,而不是一种对象。

The error you are getting when you make this change, Implicit use of 'self' in closure, refers to the invocation of the function performSegueWithIdentifier, not the sender argument.

进行此更改时出现的错误,在闭包中隐式使用'self'是指调用函数performSegueWithIdentifier,而不是sender参数。

Since you are calling the function from within a closure, Swift needs to ensure that the closure captures self i.e. prevents it from being deallocated while the closure still exists.

由于您是在闭包内调用函数,因此Swift需要确保闭包捕获self,即在闭包仍然存在时阻止它被释放。

Outside the closure this capture isn't necessary as if the object that self refers to has been deallocated the code can't be executing (The code is part of self).

在闭包之外,这种捕获不是必需的,就好像自己引用的对象已被解除分配,代码无法执行(代码是self的一部分)。

To capture self, simply refer to it inside the closure:

要捕获self,只需在闭包内引用它:

self.performSegueWithIdentifier("fromSeventhToFifth", sender: self)

or

self.performSegueWithIdentifier("fromSeventhToFifth", sender: nil)

#2


0  

AnyObject? is a optional type. You should set it nil or any instance of Class. For example:

AnyObject?是一个可选类型。您应该将其设置为nil或Class的任何实例。例如:

performSegueWithIdentifier("fromSeventhToFifth", sender: nil)
performSegueWithIdentifier("fromSeventhToFifth", sender: slef)

#3


0  

Swift 4.0, in TableView Project Template. To declare:

TableView项目模板中的Swift 4.0。声明:

// MARK: - Segues
    override func prepare(for segue: UIStoryboardSegue, sender: Any?)
    {
        if segue.identifier == "fromSeventhToFifth" {
            if let indexPath = tableView.indexPathForSelectedRow
            {

            }
        }
    }

To call:

performSegue(withIdentifier: "fromSeventhToFifth", sender: self)

#1


1  

The sender parameter is of type AnyObject? - so you can supply any object reference or nil, but you can't put AnyObject? since that is a type, not an object.

sender参数的类型是AnyObject吗? - 所以你可以提供任何对象引用或nil,但是你不能把AnyObject?因为那是一种类型,而不是一种对象。

The error you are getting when you make this change, Implicit use of 'self' in closure, refers to the invocation of the function performSegueWithIdentifier, not the sender argument.

进行此更改时出现的错误,在闭包中隐式使用'self'是指调用函数performSegueWithIdentifier,而不是sender参数。

Since you are calling the function from within a closure, Swift needs to ensure that the closure captures self i.e. prevents it from being deallocated while the closure still exists.

由于您是在闭包内调用函数,因此Swift需要确保闭包捕获self,即在闭包仍然存在时阻止它被释放。

Outside the closure this capture isn't necessary as if the object that self refers to has been deallocated the code can't be executing (The code is part of self).

在闭包之外,这种捕获不是必需的,就好像自己引用的对象已被解除分配,代码无法执行(代码是self的一部分)。

To capture self, simply refer to it inside the closure:

要捕获self,只需在闭包内引用它:

self.performSegueWithIdentifier("fromSeventhToFifth", sender: self)

or

self.performSegueWithIdentifier("fromSeventhToFifth", sender: nil)

#2


0  

AnyObject? is a optional type. You should set it nil or any instance of Class. For example:

AnyObject?是一个可选类型。您应该将其设置为nil或Class的任何实例。例如:

performSegueWithIdentifier("fromSeventhToFifth", sender: nil)
performSegueWithIdentifier("fromSeventhToFifth", sender: slef)

#3


0  

Swift 4.0, in TableView Project Template. To declare:

TableView项目模板中的Swift 4.0。声明:

// MARK: - Segues
    override func prepare(for segue: UIStoryboardSegue, sender: Any?)
    {
        if segue.identifier == "fromSeventhToFifth" {
            if let indexPath = tableView.indexPathForSelectedRow
            {

            }
        }
    }

To call:

performSegue(withIdentifier: "fromSeventhToFifth", sender: self)