[__NSCFTimer copyWithZone:]:发送到实例的无法识别的选择器 [英] [__NSCFTimer copyWithZone:]: unrecognized selector sent to instance
问题描述
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){
...
}
为什么此代码崩溃并显示错误消息:
Why this code crashes with error message:
[__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
推荐答案
定时器回调函数必须是不带参数的函数,或
a函数将 NSTimer
作为单个参数。
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回答:(以下是正确的,但不适用于此处)
Old answer: (The following is correct but does not apply here)
计时器的目标( self
在你的情况下)需要与Objective-C兼容,
即派生自 NSObject
或标有 @objc
。
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
.
另请参阅
在
使用Swift with Cocoa和Objective-C文档(强调我的)中展示Objective-C中的Swift接口:
@objc
属性使您的Swift API在Objective-C和$中可用b $ b Objective-C运行时。换句话说,您可以在任何要使用的Swift方法,属性,下标,初始值设定项,
或类之前使用@objc
属性来自Objective-C代码。如果你的
类继承自Objective-C类,编译器会为你插入属性
。
...
这个属性在你正在使用
使用选择器实现目标动作设计模式的Objective-C类 - 例如,NSTimer
或者UIButton
。
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
.
这篇关于[__NSCFTimer copyWithZone:]:发送到实例的无法识别的选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!