1

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.

@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


                }
            }
        }
    }
Wyetro
  • 8,439
  • 9
  • 46
  • 64
bibscy
  • 2,598
  • 4
  • 34
  • 82
  • You are passing the type `AnyObject?` - you need to pass an object, You could say `performSegueWithIdentifier("fromSeventhToFifth", sender: self)` or since it accepts an optional even `performSegueWithIdentifier("fromSeventhToFifth", sender: nil)` – Paulw11 Jul 06 '16 at 23:38
  • @Paulw11 if I pass `self`, I get error "Implicit use of 'self' in closure". use 'self' to make capture semantics explicit. if I pass `nil`, I get same error as above. http://i.imgur.com/lSAnK8Y.png – bibscy Jul 06 '16 at 23:45
  • 2
    You need to say `self.performSegueWithIdentifier("fromSeventhToFifth", sender: self)`. Note the use of `self` to qualify the method – Paulw11 Jul 06 '16 at 23:52
  • @Paulw11 The error has disappeared. If you put your comment in the answer section I will vote it as the correct one. Thanks – bibscy Jul 07 '16 at 00:05

3 Answers3

2

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.

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.

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.

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).

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

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

or

self.performSegueWithIdentifier("fromSeventhToFifth", sender: nil)
Paulw11
  • 108,386
  • 14
  • 159
  • 186
0

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

performSegueWithIdentifier("fromSeventhToFifth", sender: nil)
performSegueWithIdentifier("fromSeventhToFifth", sender: slef)
Lumialxk
  • 6,239
  • 6
  • 24
  • 47
0

Swift 4.0, in TableView Project Template. To declare:

// 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)
J A S K I E R
  • 1,976
  • 3
  • 24
  • 42