IOS 语法

使用  NSTimer scheduledTimerWithTimeInterval: target: selector:userInfo: repeats: 的时候有两个地方需要注意 。

  1. 首先selector指定的方法必须是带一个参数的方法,并且那个参数的类型是NSTimer *。
  2. 其次,参数是靠NSTimer 对象的userInfo属性来传递的。

举个例子:

[NSTimer scheduledTimerWithTimeInterval:0.5 
                       target:self 
                       selector:@selector(sendBroadcast:) 
                       userInfo:@"hello I'm the info to send" 
                       repeats:NO];
// sendBroadcast的定义形式应该是
-(void)sendBroadcast: (NSTimer *)timer { 

   NSString *msg = (NSString *)[timer userInfo]; // [msg isEqualToString @"hello I'm the info to send"] == YES
}
原文地址:https://www.cnblogs.com/ziyeSky/p/4156645.html