【发布时间】:2014-08-23 11:02:25
【问题描述】:
var searchDelayer:NSTimer?
func searchBar(searchBar: UISearchBar!, textDidChange searchText: String!) {
searchDelayer?.invalidate()
searchDelayer = nil
searchDelayer = NSTimer.scheduledTimerWithTimeInterval(1.5, target: self, selector: Selector("doDelayedSearch:"), userInfo: searchText, repeats: false)
}
func doDelayedSearch(text:String){
...
}
为什么这段代码会崩溃并显示错误消息:
[__NSCFTimer copyWithZone:]: unrecognized selector sent to instance
更新:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFTimer copyWithZone:]: unrecognized selector sent to instance 0x7f9c622ae7e0'
*** First throw call stack:
(
0 CoreFoundation 0x000000010c05b3e5 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x000000010ba42967 objc_exception_throw + 45
2 CoreFoundation 0x000000010c0624fd -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3 CoreFoundation 0x000000010bfba7ec ___forwarding___ + 988
4 CoreFoundation 0x000000010bfba388 _CF_forwarding_prep_0 + 120
5 CoreFoundation 0x000000010bf32935 CFStringCreateCopy + 229
6 libswiftFoundation.dylib 0x000000010dc41314 _TF10Foundation24_convertNSStringToStringFCSo8NSStringSS + 116
7 MapCode 0x000000010a1a567e _TToFC7MapCode17MapViewController15doDelayedSearchfS0_FSST_ + 62
8 Foundation 0x000000010b5fce94 __NSFireTimer + 83
9 CoreFoundation 0x000000010bfc34d4 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 20
10 CoreFoundation 0x000000010bfc3095 __CFRunLoopDoTimer + 1045
11 CoreFoundation 0x000000010bf863cd __CFRunLoopRun + 1901
12 CoreFoundation 0x000000010bf859f6 CFRunLoopRunSpecific + 470
13 GraphicsServices 0x000000010ecfd9f0 GSEventRunModal + 161
14 UIKit 0x000000010c96b990 UIApplicationMain + 1282
15 MapCode 0x000000010a1b3fee top_level_code + 78
16 MapCode 0x000000010a1b402a main + 42
17 libdyld.dylib 0x000000010f9d7145 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
【问题讨论】:
-
我已经删除了我的答案,因为它显然没有帮助。 - 再想一想,问题一定是和定时器本身有关,而不是和目标有关。能否设置一个“异常断点”,然后在异常发生时发布堆栈回溯?
-
@MartinR 谢谢。请查看更新后的帖子。当设置异常断点时,应用程序在 AppDelegate.swift 中崩溃,在线
class AppDelegate: UIResponder, UIApplicationDelegate -
您能否在调试器中单步执行并验证导致崩溃的行。堆栈跟踪看起来好像在
doDelayedSearch内部,试图复制一个字符串并找到一个意外的计时器对象。 -
定时器回调不能有
String参数。将在一分钟内更新我的答案... -
啊......那么它认为它在
CFStringCreateCopy + 229复制的字符串可能是输入参数。