Swift:"Unrecognized selector sent to instance", Xcode 9.4.1 - swift

Can anyone help me with this?
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate=self
tableView.dataSource=self
searchBar.autocorrectionType = .yes
searchBar.delegate=self
searchBarView.translatesAutoresizingMaskIntoConstraints=false
let tap:UIGestureRecognizer = UITapGestureRecognizer(target: self, action: Selector(("tapCancel:")))
searchBarView.addGestureRecognizer(tap)
tableView.addGestureRecognizer(tap)
tableView.isUserInteractionEnabled=true
}
...
func tapCancel(sender:UITapGestureRecognizer){
hideSearchBarView()
isSearchon=false
print("Tap cancel!")
}
Every time I tap the View, it crash.
"unrecognized selector sent to instance 0x7feb85d109e0"
I will appreciate any help!
Thanks!

I believe the reason is due to how method names get mapped between Swift and Objective-C, which underlies the Cocoa implementation and the whole target/action mechanism.
In your case, the Swift method:
#objc func tapCancel(sender:UITapGestureRecognizer)
...corresponds to the Objective-C selector:
-tapCancelWithSender:
Note: In order to work with the target/action paradigm (i.e., called by means of a selector), the method needs to be declared as #objc. The alternative attribute #IBOutlet (for use in conjunction with Interface Builder) also supports this. (tip of the hat to #rmaddy)
In order to remove the "withSender" part and get a selector that matches tapCancel:, you need to tell Swift to remove the argument label sender, like this:
func tapCancel(_ sender:UITapGestureRecognizer) // Notice the underscore (_)
Also, in line with the comment by #dan, perhaps you can use:
#selector(self.tapCancel(_:))
or more succinctly, as pointed by (thanks again) #rmaddy, just:
#selector(tapCancel)
(Xcode will try to autocomplete it to #selector(tapCancel(_:)), but the shorter syntax works as well and the method name is highlighted)
I was not familiar with the Selector() syntax you used, so I tried playing a bit with it, and behold:
(Selector does not match any method the compiler can "see").
(After adding "withSender", the compiler can match the method, but it suggests using the better #selector(... syntax).
As #rmaddy also pointed out in the comments, using the shorter #selector(doSomething) syntax (no colons, no underscore, no self) also does away with the problem of whether "withSender" is needed or not.

use
let tap:UIGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(tapCancel))
instead of
let tap:UIGestureRecognizer = UITapGestureRecognizer(target: self, action: Selector(("tapCancel:")))
And add #objc before method tapCancel()

Related

Swift 2: Selector in UIPinchGestureRecognizer: How to access func from another class

I'm following this class on Swift and building apps.
At 43:30 in the video, the instructor teaches how to set up a UIPinchGestureRecognizer, which takes in a function from another file in its Selector.
This is the code the instructor uses:
#IBOutlet weak var faceView: FaceView! {
didSet {
faceView.addGestureRecognizer(UIPinchGestureRecognizer(target: faceView, action: #selector(FaceView.changeScale(_:))))
updateUI()
}
}
I get 2 errors:
Expected expression in list of expressions,
and:
Expected ',' separator.
I have tried changing #selector to Selector with no luck.
the function changeScale:
func changeScale(recognizer: UIPinchGestureRecognizer)
{
switch recognizer.state {
case .Changed, .Ended:
scale *= recognizer.scale //scale is the displayed image scale
recognizer.scale = 1.0
default:
break
}
If I surround the Selector argument with quotes, the app crashes when I pinch, giving the following error:
unrecognized selector sent to instance.
I am running Xcode 8.1(8B62) and a iPhone 5 simulator on MacBook Air (Ver 100.11.6)
This is the code I used that works
faceView.addGestureRecognizer(UIPinchGestureRecognizer(target:faceView,
action:#selector(FaceView.changeScale(recognizer:))))
When you pinch the face, make sure click "Option + Left Click + Movement on your mousepad". I make the mistake of just pressing "Option + Movement on the mousepad" and it does not work.
Hope it helps.
As can be seen in the comments above, the Xcode version is 7.2 and the #selector syntax was introduced in Xcode 7.3 and therefore not available here. This just means that you should be able to use the "old" Selector syntax.
The difference is that you just give pass a strings to the Selector with the name of your function and then a : for each of the parameters your function requires. You require one parameter (the recognizer) so in your case the string looks like this:
"changeScale:"
So you'd use:
Selector("changeScale:")
And you end up with:
#IBOutlet weak var faceView: FaceView! {
didSet {
faceView.addGestureRecognizer(UIPinchGestureRecognizer(target: faceView, action: Selector("changeScale:")))
}
}
As you can see, this is error prone...one typo and kaboom! Which is why the new #selector syntax is a fine improvement...sorry...not trying to rub it in.
Hope this helps you.

Issue with types in swift?

I did a Team Treehouse code challenge, and I had an issue that I wanted to ask. Basically I created a func inside an enum that takes the specific case of the enum and initializes a given object. It told me to create it inside the enum, but unfortunately when I do this, I came across some errors. I was able to finish the challenge by doing it outside the enum, but I don't think that was exactly what I was supposed to do.
Anyways, my problem was that when it returned the object UIBarButtonItem, if it was assigned to a constant, the type of the constant is "() -> UIBarButtonItem" or depending on how the switch is setup, it could also come out like "(Button) -> UIBarButtonItem". I'm not really sure what the '->' symbol means in this case. Why does it not completely change into the class? What is going on here? In the tutorial right before this, it seems we did the same exact thing, so I'm not sure why it doesn't work.
enum Button {
case Done(String)
case Edit(String)
func toUIBarButtonItem () -> UIBarButtonItem {
switch self {
case .Done: return UIBarButtonItem(title: "Done", style: .Done, target: nil, action: nil)
case .Edit: return UIBarButtonItem(title: "Plain", style: .Plain, target: nil, action: nil)
}
}
}
let done = Button.Done("Done")
let doneButton = Button.toUIBarButtonItem(done)
This is the code that I am running, not including the UIBarButtonItem class. I hope this is enough information to understand what I am asking.
You are calling the toUIBarButtonItem() method on the Button type instead of a Button instance; this is why it does something special: it returns a curried function, which you then bind to done. The result, however, is still a function.
What you probably mean to do is invoke the method on your done object:
let done = Button.Done("Done")
let doneButton = done.toUIBarButtonItem()
A more in-depth explanation can be found under http://oleb.net/blog/2014/07/swift-instance-methods-curried-functions/

Understanding Swift 2.2 Selector Syntax - #selector()

I am switching over the syntax of my project toward Swift 2.2 (which xCode helps me do automatically); however, I do not understand the new #selector() syntax.
As an example:
timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self,
selector: #selector(MyVC.timerCalled(_:)), //new selector syntax!
userInfo: nil, repeats: true)
This has the selector #selector(MyVC.timerCalled(_:))
What does the _: signify? Can you add other variables into this selector? Say, #MyVC.timerCalled(_:whateverVar).
General info on what is different in this syntax as opposed to the string based implementation from earlier versions of Swift are greatly appreciated.
The bit in parenthesis is a mechanism for identifying the argument list for the selector that you want.
I recommend you look at the Generalized Naming proposal from Swift Evolution. It covers cases where you have a number of functions that differ only by their parameter labels and need to refer to them. The example from that document is:
extension UIView {
func insertSubview(view: UIView, at index: Int)
func insertSubview(view: UIView, aboveSubview siblingSubview: UIView)
func insertSubview(view: UIView, belowSubview siblingSubview: UIView)
}
If you wanted to get a function value for one of those the result is ambiguous:
let fn = someView.insertSubview // ambiguous: could be any of the three methods
The solution implemented is to add the argument labels, without any type information to the code that generates the function value to disambiguate which you want:
let fn = someView.insertSubview(_:at:)
let fn1 = someView.insertSubview(_:aboveSubview:)
See how the labels are added in the parens?
This proposal played a role in the one that most directly applies to your question:
Referencing the Objective-C selector of a method
In this particular case the selector you want to refer to is timerCalled: which is a function of one parameter that has no label. Hence (_:). The underscore means the label is not specified and the colon.
Swift 2.2 has deprecated Stringified selectors: In swift 2.0, we use to write the selector as a String i.e "buttonClicked". The disadvantage with this approach is that the compiler can't check whether the method really exists or not at compile time(Even if you have misspelled it).
EX:1
func buttonClicked(){
}
So the above method in the new approach can be called as #selector(buttonClicked)
EX:2
func buttonClicked(stringValue : String){
}
So the above method in the new approach can be called as #selector(buttonClicked(_:))
EX:3
func buttonClicked(stringValue : String, indexValue : Int){
}
So the above method with parameters in the new approach can be called as #selector(buttonClicked(_:indexValue:))
Consider below code for adding target to button in swift 3 using #selector
button.addTarget(self, action: #selector(self.buttonAction(sender:)),
for: UIControlEvents.touchUpInside)
func buttonAction(sender:UIButton!){
}
This syntax worked for me when migrating to swift 3
This is the way Swift method signatures are represented in documentation, and it is now beginning to be used in new language features such as the #selector() syntax to express methods by their argument lists.
The each colon (:) represents a method parameter. For named parameters, the colon is preceded by the external parameter name; for unnamed parameters, an underscore (_) is used.
So for example, MyVC.timerCalled(_:)) indicates a method on the MyVC type with one unnamed parameter, which might be declared like this:
func timerCalled(timer: NSTimer) { ... }
(Note that timer is the internal parameter name, since by default the first parameter of a method is unnamed)
The type (MyVC in your example) can also be omitted if it is within the same scope of the #selector() declaration.
A more complex example might look like this:
let sel = #selector(aMethodWithSeveralParameters(_:secondParam:thirdParam:))
...
func aMethodWithSeveralParameters(firstParam: Int, secondParam: Int, thirdParam: Int) { ... }

Swift 2 unrecognized selector in tapgesture

I recently started to update my app to Swift 2.0, but I've run into a problem that has a hundred different answers on SO, none of which seemed to be related to my problem.
(This worked before updating the application to Swift 2.0), but I havn't been able to find any changes made to tap gesture recognisers?
Here's the full error I'm getting:
[Stocks.SidePanelViewController proFormulaTapGesture]: unrecognized selector sent to instance 0x14f06ae00
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Stocks.SidePanelViewController proFormulaTapGesture]: unrecognized selector sent to instance 0x14f06ae00'
*** First throw call stack:
(0x1830904d0 0x197afff9c 0x1830971ec 0x183094188 0x182f9920c 0x188c3c58c 0x188899b50 0x18872d104 0x188c3bc30 0x1886ed28c 0x1886eb3ac 0x18872b088 0x18872a5ec 0x1886fb908 0x1886f9fac 0x183047d6c 0x183047800 0x183045500 0x182f75280 0x18e0ec0cc 0x188762df8 0x100158184 0x1983428b8)
libc++abi.dylib: terminating with uncaught exception of type NSException
It's a simple tap gesture. But it seems to not recognize the selector anymore.
Here's the code I use to set up the recognizer:
let proFormulaTap = UITapGestureRecognizer()
proFormulaTap.addTarget(self, action:"proFormulaTapGesture")
proFormulaView.addGestureRecognizer(proFormulaTap)
Here's the function I'm trying to run:
func proFormulaTapGesture() throws {
print("proFormulaTapGesture")
selectView(proFormulaView)
selectedMenuItem = 0
Formula = 1
menuTabBarController.selectedIndex = 0
navigationController?.navigationBar.topItem!.title = "BASIC FORMULA"
try (menuTabBarController.viewControllers as! [SuggestionsViewController])[0].loadSuggestions()
}
However since it never prints "proFormulaTapGesture" in the console. I definitely think the error occurs before the function. Also suggested by the error mentioning the selector.
Obviously try/catch has been added to the function since the update to Swift 2.0, but nothing has been changed in the setup of the tapGestureRecognizer.
I tried removing the "throws" and try from the function, but the problem perists. I also tried making a button instead of a tap gesture recognizer. But I'm still getting the same error, suggesting it might be a problem with the selector (function) and not the tap gesture / button. However all my other buttons in my app works just fine?
I also tried to rename the selector/function, and the tap gesture recognizer. Still the same.
The original Code was programmed in Swift, not Obj-C. The throws and try, was added during Apple's code conversion to Swift 2.0
Any help to why this is suddenly broken would be greatly appreciated.
Thanks!
The issue may be that UIKit does not know how to call a selector that throws, at least in this context.
Remember that UIKit will be calling this function in response to a tap action. It is not accustomed to calling a function that throws; obviously the throw must alter the method's call signature as far as the Objective-C runtime is concerned. Beyond that, if your function proFormulaTapGesture did throw an error, what would UIKit do with the error even if it could catch it?
I'm not sure where you added do/try/catch. But unless it was inside your action function proFormulaTapGesture, I do not see how it could be relevant. I believe that you will need to implement the try/catch error handling inside proFormulaTapGesture, so that it effectively swallows the error, so that proFormulaTapGesture does not throw.
I just created a quick test project myself and found that a throwing target action gives the same "Unrecognized selector" error you got:
override func viewDidLoad() {
super.viewDidLoad()
self.tapper = NSClickGestureRecognizer(target: self, action: "didClick")
self.view.addGestureRecognizer(self.tapper!)
}
func didClick() throws {
print("did click")
}
2015-07-27 00:16:43.260 Test1[71047:54227175] -[Test1.ViewController didClick]: unrecognized selector sent to instance 0x6000000c5010
...
I believe you will need to rework your code to handle the error internally in your action function so that it does not throw - something like this:
func proFormulaTapGesture() {
print("proFormulaTapGesture")
selectView(proFormulaView)
selectedMenuItem = 0
Formula = 1
menuTabBarController.selectedIndex = 0
navigationController?.navigationBar.topItem!.title = "BASIC FORMULA"
do {
try (menuTabBarController.viewControllers as! [SuggestionsViewController])[0].loadSuggestions()
} catch {
// Handle the error here somehow
}
}
If you are unable to handle the error within proFormulaTapGesture, you'll have to be more creative with passing it out of the function (think NSNotification, etc...).
To be clear, I created a new Single View iOS application in Xcode 7 beta 4, and used this exact code as my ViewController. No other changes. The app compiles and works just fine in the iOS simulator.
#robertvojta also points out that if didTap below is marked private, #objc is also required to avoid a dynamic dispatch crash.
import UIKit
class ViewController: UIViewController {
var tapper: UITapGestureRecognizer?
override func viewDidLoad() {
super.viewDidLoad()
self.tapper = UITapGestureRecognizer(target: self, action: "didTap")
self.view.addGestureRecognizer(self.tapper!)
}
func didTap() {
print("did Tap")
}
}
(Note I also tested on an OS X project, which is the code I used earlier in the answer).

Swift Selector with default argument

I have Write simple Code here
self.navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Cancel, target: self, action: Selector("cancelClick"))
Actual Function
func cancelClick(isAlert:String = "yes"){
self.dismissViewControllerAnimated(true, completion: { () -> Void in
if isAlert == "yes" {
Functions.displayAlert("called")
}
})
}
self.cancelClick() - Worked but if i didn't pass the argument
self.cancelClick(isAlert:"no") - Crashed
So what should be my selector if i have to pass argument in default perameter
tried with both Selector("cancelClick") and Selector("cancelClick:") but no luck.
The thing is that the parameter is not up to you. It is always the button (the "sender"), and that is the only thing it can be.
In other words, if you want this function to have a parameter, then by all means you will need to set your selector string as "cancelClick:" - the colon means that it takes a parameter. But that parameter must be the button:
func cancelClick(bbi:UIBarButtonItem?) {
However, you will notice that I have cleverly made this UIBarButtonItem parameter an Optional. Why do you think I did that? Because now you can also call it directly and pass nil:
self.cancelClick(nil)
Thus, cancelClick: now has a way to know whether the call comes from the tapping of a button or by a direct call - if bbi is not nil, the button was tapped; if bbi is nil, we were called directly from code. Sneaky, eh?
Another sneaky approach is to make the parameter an AnyObject:
func cancelClick(sender:AnyObject) {
The beauty of this is that you can call it with any kind of class instance. cancelClick can check the type of the sender. If it is a UIBarButtonItem (sender is UIBarButtonItem), then we were called by tapping the button. Otherwise, if called in code, you can pass in a string or anything else that this function might be prepared to deal with.
Don't use Selector("cancelClick:"), try instead just "cancelClick:" with the colon so you can pass an argument.
And as Matt said, the argument you must pass is the sender (the button) itself.