NSTimer在触发时导致"无法识别的选择器"崩溃

Sci*_*y77 3 objective-c selector nstimer ios

我正在使用a NSTimer来运行动画(现在只是调用它myMethod).但是,它导致了崩溃.

这是代码:

@implementation SecondViewController


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.


- (void) myMethod
{
    NSLog(@"Mark Timer Fire");

}


- (void)viewDidLoad
{ 
[super viewDidLoad];



NSLog(@"We've loaded scan");

[NSTimer scheduledTimerWithTimeInterval:2.0
                                 target:self
                               selector:@selector(myMethod:)
                               userInfo:nil
                                repeats:YES];

animationTimer = [NSTimer scheduledTimerWithTimeInterval: 1.0 target:self selector:@selector(myMethod:) userInfo:nil repeats: YES];


}
Run Code Online (Sandbox Code Playgroud)

这是崩溃期间的输出

- [SecondViewController myMethod:]:无法识别的选择器发送到实例0x4b2ca40 2012-06-21 12:19:53.297 Lie Detector [38912:207] *由于未捕获的异常'NSInvalidArgumentException'终止应用程序,原因:' - [SecondViewController myMethod:] :无法识别的选择器发送到实例0x4b2ca40'

那么我在这里做错了什么?

Dr.*_*enn 5

我在使用Swift时遇到了这个问题.在Swift中我发现NSTimer的目标对象必须是NSObject,这可能并不明显.

class Timer : NSObject {
   init () { super.init() }
   func schedule() {
      NSTimer.scheduledTimerWithTimeInterval(2.0,
                             target: self,
                             selector: "myMethod",
                             userInfo: nil,
                            repeats: true)
  }
  func myMethod() {
     ...
  }
}
Run Code Online (Sandbox Code Playgroud)

希望这有助于某人.