override 'shouldPerformSegueWithIdentifier' function throws error
override 'shouldPerformSegueWithIdentifier' function throws error
I am trying to prevent all segues if a certain condition is not met. This is how I have approached this problem:
override func shouldPerformSegueWithIdentifier(identifier: String, sender: AnyObject?) -> Bool
if condition == false
print("Segue wont occur")
return false
else
print("Segue will occur")
return true
Though this solution seems to work for many people, I cannot compile, and I am thrown the error:
Method does not override any method from its superclass
The override function is located within the UIViewController
superclass.
UIViewController
I am using Swift 4, and I have searched thoroughly for a solution. Any help would be appreciated.
override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool
An easy way to figure out yourself is to comment out the entire method, retype shouldPerf and use code completion.
– vadian
Sep 16 '18 at 13:14
@vadian thank you for that advice. I was stupid enough not to see that I had the ending wrong. Thank you for the time
– Caspar B
Sep 16 '18 at 13:24
@matt thank you for that answer. What do you mean when you say I am not using Swift 4?
– Caspar B
Sep 16 '18 at 13:26
@matt Okay, well I think we established that I mistakenly used a outdated function name. Doesn’t mean the rest of the code is Swift 1. I am programming in Swift 4.2, but a code snipped I used happened to be outdated and I didn’t realise. I think that’s where the confusion came from.
– Caspar B
Sep 16 '18 at 13:31
1 Answer
1
You seem to have gotten the method signature for shouldPerformSegue
wrong.
shouldPerformSegue
override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool
if condition == false
print("Segue wont occur")
return false
else
print("Segue will occur")
return true
Thank you. I made a mistake and was stupid enough not to realise this.
– Caspar B
Sep 16 '18 at 13:22
When overriding methods, it is always better to let autocomplete do the job like @vadian has stated in his comment. No way of going wrong then.
– Rakesha Shastri
Sep 16 '18 at 13:22
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy
“I am using Swift 4” no you aren’t. “and I have searched thoroughly for a solution” no you haven’t. That’s the problem. Look at the docs developer.apple.com/documentation/uikit/uiviewcontroller/… Copy and paste! It’s
override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool
– matt
Sep 16 '18 at 13:11