[__NSCFTimer copyWithZone:]: unrecognized selector sent to instance
The timer callback function must be either a function without arguments, or
a function taking a NSTimer
as a single argument.
In your case, it should be
func doDelayedSearch(timer: NSTimer) {
let searchText = timer.userInfo as String
// ...
}
Old answer: (The following is correct but does not apply here)
The target of the timer (self
in your case) needs to be compatible with Objective-C,
i.e. derived from NSObject
or marked with @objc
.
See also
Exposing Swift Interfaces in Objective-C in the
"Using Swift with Cocoa and Objective-C" documentation (emphasis mine):
The
@objc
attribute makes your Swift API available in Objective-C and
the Objective-C runtime. In other words, you can use the@objc
attribute before any Swift method, property, subscript, initializer,
or class that you want to use from Objective-C code. If your class
inherits from an Objective-C class, the compiler inserts the attribute
for you.
...
This attribute is also useful when you’re working with
Objective-C classes that use selectors to implement the target-action design pattern—for example,NSTimer
orUIButton
.
Cocoa NSOutlineView bug - [NSCFTimer copyWithZone:]: unrecognized selector sent to instance
Sounds like an object is dying prematurely. You get the “unrecognized selector sent to instance” exception when a new object is allocated later with the same pointer and then something tries to send the old object a message (in the example shown, the reincarnation is an NSTimer and the message something tried to send the previous object was copyWithZone:
). You get an EXC_BAD_ACCESS
crash when the object is simply garbage memory.
Debug this by running your app under Instruments with the Zombies instrument enabled. The object will, instead of dying, become a zombie object. When something tries to send a zombie a message, the zombie will moan (figuratively speaking), which will show up in Instruments's timeline as a flag. You can click a button in that flag to view the history of the object, including all of its retentions and releases.
Unrecognized selector sent to instance with NSTimer.scheduledTimerWithTimeInterval function
You selector is "runCountDownFunction:"
. The colon is meaningful; it means this method takes one parameter. So you must make your method take one parameter. Declare your method like this:
func runCountDownFunction(timer:NSTimer) {
Now its signature matches the selector you gave when you created the timer.
Objective-C - NSTimer: unrecognized selector sent to class - call method from another class
You need to specify an instance of the Food
class, so this argument is incorrect:
target:[Food class]
So what you are passing to NSTimer
is a Class
object, not a Food
object.
Instead you probably need an instance variable of the Food
instance and specify that:
@interface MyClass ()
{
Food *_food;
}
@implementation MyClass
...
- (void)whatever
{
_food = [Food new]; // This might need to be elsewhere
SEL selector = @selector(addCloudOne);
[NSTimer scheduledTimerWithTimeInterval:k1
target:_food
selector:selector
userInfo:nil
repeats:YES];
}
Swift NSTimer unrecognized selector sent to instance timerFireMethod
You've made a simple, yet common mistake.
Your method signature should be:
func timerFire(timer: NSTimer) {}
And your timer setup should be:
NSTimer(timeInterval: 1.0, target: self, selector: "timerFire:", userInfo: nil, repeats: true)
The mistake is that you're missing the colon in the selector name. timerFire
is different from timerFire:
. Skip the colon and it'll look for for a method like this:
func timerFire() {}
Without the NSTimer parameter. It's best though to include the parameter, and thus the colon, so that you can confirm the timer you get is the one you expect.
The same is true for notifications. If you're using Notification Center, include the colon, and the Notification object in the method.
Related Topics
Using "Codable" to Set Property Values Doesn't Work Through Inheritance
Swipe Gesture Interrupts Uislider Control in iOS 13, But Not Previous iOS Versions
How to Display Current Time (Realtime) in iOS 14 Home Widget
With Swiftui, How to Constrain a View's Size to Another Non-Sibling View
Is There a Number Type with Bigger Capacity Than U_Long/Uint64 in Swift
JSONencoder and Propertylistencoder Don't Conform to Encoder
How to Get Rid of Array Brackets While Printing
Swift Protocol Property in Protocol - Candidate Has Non-Matching Type
Observe Progress of Data Download in Swift
How to Pass a Reference to a Boolean Rather Than Its Value
Swift 3: Int Is Not Convertible to Bool in Bitwise Operation
Limiting Concurrent Access to a Service Class with Rxswift
What Does T Equals T Mean in Generics
Inferred to Have Type 'Anyclass', Which May Be Unexpected
How to Encode and Decode the Closures