1
votes

I am trying to confirm submission before leaving the view controller :

however I am getting this error :

type () does not conform to protocol anyobject

here is the line where the error appears :

        self.navigationItem.backBarButtonItem?.target = self.validateBeforeBack("back", completion: { (bool) -> () in
        self.navigationController?.popViewControllerAnimated(true)
    })

UDATE : So the solution was to change target to action and to put the function call inside Selector()

that is :

    self.navigationItem.backBarButtonItem?.action = Selector(self.validateBeforeBack("back", completion: { (bool) -> () in
        self.navigationController?.popViewControllerAnimated(true)
    }))
2
put your delegate method @optional.Chetan Prajapati
@ChetanPrajapati I am not using delegate methods for this issue , Can u explain what func R U meaningiShaalan

2 Answers

3
votes

It's unclear what validateBeforeBack() returns, since you did not provide it. From the error, I assume it returns () (i.e. "void" or "nothing"). You can't assign the result of that to a property.

I assume what you really wanted to do was call this function when the button is pressed. That's not how target/action buttons work. See Target-Action in Concepts in Objective-C Programming. You need to set target to the object you want the message sent to, and action to the selector you want to send.

2
votes
  1. popViewControllerAnimated(_:) has a return type of UIViewController?, however the closure argument to your function validateBeforeBack(_:completion:) has a Void return type (the implementation of your validateBeforeBack(_:completion:) function, or else the closure that you pass to it, should be adjusted to take this into account)

  2. make sure that your validateBeforeBack(_:completion:) function actually has a return type (since it is the value in an assignment)