2011-09-04 58 views
0

掃描陣列我有一個數組,數組的每個對象是具有2個對象:以毫秒爲單位 雙=時間 的NSString =字符串打印如何按時間

,我想我的應用程序將掃描陣列當它達到毫秒時,它將打印nsstring。

+0

不確定你的意思是「什麼時候會到毫秒級」。你的意思是如果當前時間有相同的毫秒數?如果時間確切,這可能有點過於精確。它可以在一段時間內嗎? – msgambel

+0

是的,它不需要完全在相同的毫秒 – MTA

回答

2

好吧,我假定對象被稱爲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); 
    } 
    } 
} 

在您的.h文件中,添加一行:

NSDate * startDate; 

然後,在您的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得到一個NSDate0毫秒)最後,在dealloc方法,添加:

[startDate release]; 

希望幫助!

+0

謝謝,這是非常有益的 – MTA

+0

沒問題!很高興我能幫上忙! – msgambel