In my SKScene subclass I have implemented a touchesBegan method. This method had the NSSet changed to Set in order to make it Swift 1.2 compatible (see this question).
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
// ...
}
Now the compiler gives me an error: Method does not override any method from its superclass. My code -as any Swift code- was broken in 1.2, and I have fixed every issue except this override case. Am I missing something here?
This worked for me
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
//...
}
For more information about why is happening this error you can read from this answer that explain in detail about this: https://stackoverflow.com/a/30892467/2091181
So the "problem" had everything to do with the fact that the project had a Set class in it implemented, and I did not detect this redundancy. Quite a silly mistake.
Related
I was looking at a project that I made in Swift 1 a few years ago. And I noticed an error after converting my code to Swift 3 syntax that said Method does not override any method from its superclass. I know its because the Set is old syntax but what do I replace it with? This is the line:
override func touchesBegan(_ touches: Set<NSObject>, with event: UIEvent) {
self.view.endEditing(true)
}
This method in Swift 3 has change to:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesBegan(touches, with: event)
}
Reference from Apple
Swift 3
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesBegan(touches, with: event)
self.view.endEditing(true)
}
I am relatively new to Swift and I am following some basic tutorials but I seem to be having a problem with some methods which attempt to allow the user to press return to minimise the keyboard or to click off the keyboard and the keyboard will disappear, I understand why I am receiving these errors but have no clue how to go about fixing it, I feel something may have been changed in the newer version of Swift I am using as he is using an older version than me, could anyone possibly explain how to go about fixing these two errors please? Any help would be greatly appreciated here is my source code: (First error, value of type 'viewController' has no member 'text' and secondly, touchesBegan method does not override any method from its superclass)
import UIKit
class ViewController: UIViewController {
#IBAction func buttonPressed(sender: AnyObject) {
label.text = textArea.text
}
#IBOutlet weak var textArea: UITextField!
#IBOutlet weak var label: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.text.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
self.view.endEditing(true)
}
func textFieldShouldReturn(textField: UITextField!) -> Bool {
textField.resignFirstResponder()
return true
}
}
You have 2 problems here, based on the images you posted:
1) The method touhesBegan you are using is not correct:
Correct one:
func touchesBegan(_ touches: Set<UITouch>, withEvent event: UIEvent?)
Yours:
func touchesBegan(touches: NSSet, withEvent event: UIEvent)
I think you want a delegate for the UITextField, so this one is not corerct: touchesBegan is a method for the UIReponder delegate and not for UITextFieldDelegate.
Here you can find the reference for the UITextFieldDelegate.
2) the variable text doesn't exists in your code. I think you wanted to use textArea instead.
Hope this can help you, happy coding!
In your case change following thing:
instead of :
self.text.delegate = self
change :
self.textArea.delegate = self
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
}
And for delegate add like this
class ViewController: UIViewController, UITextFieldDelegate {
}
I try to hide keyboard by pressing on any place of the screen. I use following code
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
self.view.endEditing(true)
}
But id doens't work, keyboard is not hiding.
How to use it correctly ?
Conform to UITextFieldDelegate
In your viewController() assign delegate of YOURTextField.delegate = self.
In your method use self.YOURTextField.resignFirstResponder()
After switching to Swift 2.0
override public func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
userInteractionBegan(touches.first as! UITouch)
}
produces an error message:
Method class does not override any method from its superclass
I have no idea why override does not override anymore!
In Swift 2, there are changes in the touchesBegan method. Now the first parameter is Set<UITouch> instead of NSObject. So Swift tells you that you try to override a method which doesn't exist. Use Set<UITouch> instead:
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
^^^^^^^
}
In swift the method signature is changed to become more "swiftier". This is the new method signature you should overrride:
override public func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
}
I have a function that that receives a Set<NSObject> and I need to iterate over the set as a Set<UITouch>. How exactly do I test for this and unwrap the set?
override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {
for touch in touches {
// ...
}
}
Generally you would use a conditional cast to check each element
for its type. But here, the touches parameter is
documented
as
A set of UITouch instances that represent the touches that are moving
during the event represented by event.
therefore you can force-cast the entire set:
override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {
for touch in touches as! Set<UITouch> {
// ...
}
}
Note that in Swift 2 the function declaration changed to
func touchesMoved(_ touches: Set<UITouch>, withEvent event: UIEvent?)
(due to the "light-weight generics" in Objective-C) so that a cast is not needed anymore.
Use the as operator to perform type casting:
for touch in touches {
if let aTouch = touch as? UITouch {
// do something with aTouch
} else {
// touch is not an UITouch
}
}