Swift - 方法存在,但错误:无法识别的选择器发送到实例

时间:2015-03-24 03:19:27

标签: swift selector nstimer

class MCPoll: NSObject {

    private var pollTimer: NSTimer!

    func startPoll(){
        pollTimer = NSTimer.scheduledTimerWithTimeInterval(0.2, target: self, selector: Selector("poll"), userInfo: nil, repeats: false)
    }

    private func poll(){ }
}

“poll”是一个无参数函数,但是xcode提示:

  

2015-03-24 11:08:20.478 mobile [7897:1138436] - [mobile.MCPoll民意调查]:   无法识别的选择器发送到实例0x174276940

我项目中的其他类使用“NSTimer”,但没有问题。

此崩溃日志:

Last Exception Backtrace: 
0   CoreFoundation       0x186e96530 __exceptionPreprocess + 132
1   libobjc.A.dylib      0x197e6c0e4 objc_exception_throw + 60
2   CoreFoundation       0x186e9d5f4 -[NSObject(NSObject) doesNotRecognizeSelector:] + 220 
3   CoreFoundation       0x186e9a3ac ___forwarding___ + 928 
4   CoreFoundation       0x186d9ec4c _CF_forwarding_prep_0 + 92
5   Foundation           0x187d8f514 __NSFireTimer + 92 
6   CoreFoundation       0x186e4ec20  __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 28 
7   CoreFoundation       0x186e4e8d0 __CFRunLoopDoTimer + 888 
8   CoreFoundation       0x186e4c31c __CFRunLoopRun + 1372 
9   CoreFoundation       0x186d791f4 CFRunLoopRunSpecific + 396
10  GraphicsServices     0x19019b6fc GSEventRunModal + 168
11  UIKit                0x18b70a10c UIApplicationMain + 1488 
12  MeChat               0x1001016dc top_level_code (AppDelegate.swift:0) 
13  MeChat               0x10010171c main (AppDelegate.swift:0) 
14  libdyld.dylib        0x1984eaa08 start + 4

2 个答案:

答案 0 :(得分:7)

如果您想保留private功能而不是将其公开为publicinternal,您还可以添加@objc属性。此属性将方法公开给Objective-C运行时。所以你的方法声明是

@objc private func poll(){ }

答案 1 :(得分:0)

private功能中删除poll