Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift [\uu NSCFTimer copyWithZone:]:发送到实例的选择器无法识别_Swift_Nstimer_Ios8 - Fatal编程技术网

Swift [\uu NSCFTimer copyWithZone:]:发送到实例的选择器无法识别

Swift [\uu NSCFTimer copyWithZone:]:发送到实例的选择器无法识别,swift,nstimer,ios8,Swift,Nstimer,Ios8,此代码崩溃的原因及错误消息: var searchDelayer:NSTimer? func searchBar(searchBar: UISearchBar!, textDidChange searchText: String!) { searchDelayer?.invalidate() searchDelayer = nil searchDelayer = NSTimer.scheduledTimerWithTimeInte

此代码崩溃的原因及错误消息:

    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

计时器回调函数必须是没有参数的函数,或者 将NSTimer作为单个参数的函数

对你来说,应该是这样

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
旧答案:以下是正确的,但不适用于这里

在您的情况下,计时器自身的目标需要与Objective-C兼容, i、 e.从NSObject派生或标记为@objc

另见 在 将Swift与Cocoa和Objective-C文档一起使用,重点是:

@objc属性使Swift API在Objective-C和 Objective-C运行时。换句话说,您可以使用@objc 任何Swift方法、属性、下标、初始值设定项之前的属性, 或要从Objective-C代码中使用的类。如果你的班级 从Objective-C类继承,编译器插入属性 为你。 ... 使用时,此属性也很有用 Objective-C类使用选择器实现目标动作设计模式,例如NSTimer或UIButton


在我的例子中,self属于UIViewController类。我将其标记为@objc,但崩溃仍在发生。我已删除我的答案,因为它显然没有帮助。-再想一想,问题一定与计时器本身有关,而不是与目标有关。您能设置一个异常断点,然后在异常发生时发布堆栈回溯吗?@MartinR谢谢。请参阅更新后的帖子。设置异常断点后,AppDelegate.swift中的应用程序崩溃,在线类AppDelegate:UIResponder,uiapplicationelegate您可以在调试器中单步执行并验证导致崩溃的行。堆栈跟踪看起来好像在doDelayedSearch中,试图复制字符串,却找到了意外的计时器对象。计时器回调不能有字符串参数。将在一分钟内更新我的答案…啊…那么它认为在CFStringCreateCopy+229复制的字符串可能是输入参数。
func doDelayedSearch(timer: NSTimer) {
    let searchText = timer.userInfo as String
    // ...
}