Timer函数致应用崩溃 请求排查‘unrecognized selector sent to instance’错误
Hey there! Let’s break down these two frustrating problems step by step—they’re totally fixable once we narrow down the root causes.
1. Timer Causing App Crashes
The #1 culprit here is almost always a strong reference cycle (retain cycle) between your Timer and the object it’s attached to (like a ViewController). Here’s the cycle:
- The Timer holds a strong reference to its target (your ViewController)
- Your ViewController likely stores the Timer as a strong property
- Neither object gets deallocated, leading to memory leaks, and eventually crashes when the Timer tries to fire on a "zombie" deallocated object.
Fixes to Try:
- Use the block-based Timer API with a weak self reference to break the cycle:
// Swift example Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { [weak self] timer in guard let self = self else { timer.invalidate() // Clean up if self is already gone return } self.yourTimerTask() }// Objective-C example __weak typeof(self) weakSelf = self; self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 repeats:YES block:^(NSTimer *timer) { typeof(self) strongSelf = weakSelf; if (!strongSelf) { [timer invalidate]; return; } [strongSelf yourTimerTask]; }]; - Always invalidate the Timer when the parent object is deallocated:
deinit { timer?.invalidate() } - Avoid running Timers on background threads unless you explicitly tie them to a active runloop—Timers on inactive threads can fire unexpectedly or cause crashes.
2. "Unrecognized Selector Sent to Instance" Crash
This error means an object received a method call it doesn’t implement (or can’t access). Here’s how to track it down fast:
First, Parse the Crash Log
Look for lines like:
-[YourViewController handleButtonTap:] unrecognized selector sent to instance 0x123abc
This tells you exactly which object (the hex ID) and which method (handleButtonTap:) is causing the problem.
Common Causes & Fixes:
- Typos in method signatures: Double-check that the selector you’re calling matches the method’s exact name, including colons for parameters. For example, if you defined
func handleButtonTap(), but tried to callhandleButtonTap:(with a colon), this error will happen. - Zombie Object: The object was already deallocated, but a pointer still points to its old memory address. Enable Zombie Objects in Xcode’s scheme (Edit Scheme > Run > Diagnostics > Check "Zombie Objects")—this will flag when a deallocated object gets a message.
- Missing implementations: If you declared a method in a header (Objective-C) or protocol, make sure you actually wrote the code for it in the class. For Swift, mark methods with
@objcif you’re calling them from Objective-C or usingperformSelector. - Category conflicts: If the method is in a category, ensure the category’s header is imported correctly, and there’s no name collision with another class’s method.
Debugging Pro Tip:
Set an Exception Breakpoint in Xcode (Debug > Breakpoints > Create Exception Breakpoint). This will pause execution exactly when the unrecognized selector is sent, so you can trace back where the faulty call originated.
内容的提问来源于stack exchange,提问作者Olof.W

