Iphone 如何按时间扫描阵列

Iphone 如何按时间扫描阵列,iphone,objective-c,Iphone,Objective C,我有一个数组,数组中的每个对象都有两个对象: double=时间(毫秒) nsstring=要打印的字符串 我希望我的应用程序扫描数组,当它达到毫秒时,它将打印nsstring。好的,我假设对象名为MyObject,数组名为objectArray,请尝试以下操作: - (void)checkArrayForTimes; { float epsilon = 0.1; NSDate * currentTime = [NSDate date]; float milliseconds = [

我有一个数组,数组中的每个对象都有两个对象: double=时间(毫秒) nsstring=要打印的字符串


我希望我的应用程序扫描数组,当它达到毫秒时,它将打印nsstring。

好的,我假设对象名为
MyObject
,数组名为
objectArray
,请尝试以下操作:

- (void)checkArrayForTimes; {
  float epsilon = 0.1;
  NSDate * currentTime = [NSDate date];
  float milliseconds = [currentTime timeIntervalSinceDate:startTime];
  milliseconds -= (int)milliseconds; // This mods out all of the seconds so you are only working with milliseconds.
  for(MyObject * myObject in objectArray){
    if(fabs(myObject.time - milliseconds) < epsilon){
      NSLog(@"%@", myObject.string);
    }
  }
}
然后,在
viewDidLoad:
方法中,添加以下行:

    // Note, you can change the 0.05 to fit your needs.
    startDate = [[NSDate date] retain];
    [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(checkArrayForTimes) userInfo:nil repeats:YES];
注意:,您可能需要使用
NSDateFormatter
0
毫秒的时间获取
NSDate
。)最后,在
dealoc
方法中,添加:

[startDate release];

希望有帮助

不知道你所说的“何时会达到毫秒级”是什么意思。你的意思是如果当前时间的毫秒数相同?如果时间是精确的,这可能有点太精确了。它能在某个时间间隔内吗?是的,它不需要完全在同一毫秒内谢谢你这很有用没问题!很高兴我能帮忙!
[startDate release];