2011-05-26 105 views
0
-(void)insertEvent:(stRs232Timer*)pEvent 
{ 
    BOOL bFound = NO; 
    NSLog(@"insertEvent"); 
    pEvent->uExpirationTime = pEvent->uPeriod-45; 

    // Insert the item into the event queue in chronological order 
    int no = [m_cPendingEventList count]; 
    stRs232Timer* val; 
    for(int i=0;i<no;i++) 
    { 
     val = (stRs232Timer*)[m_cPendingEventList objectAtIndex:i]; 
     if (pEvent->uExpirationTime < val->uExpirationTime) 
     { 
      NSLog(@"Going to insert!!"); 
      if (i=0) { 
       [m_cPendingEventList insertObject:(void*)pEvent atIndex:i]; 
       bFound = YES; 
       break; 
      } 
      else //Insert before 
      { 
       [m_cPendingEventList insertObject:(void*)pEvent atIndex:(i-1)]; 
       bFound = YES; 
       break; 
      } 
     } 
     i++; 
    } 
    if (!bFound) { 
     [m_cPendingEventList insertObject:(void*)pEvent atIndex:(no+1)];//Insert last 
    } 
} 

這是以正確的順序搜索和插入事件的正確方法嗎?插入陣列

我在上面的if()stmts中獲得運行時間休息。

回答

2

爲什麼不直接使用[array addObject:obj];

你並不需要指定一個指數 - 它會在數組的末尾插入。

+0

這就是我正在做的insertbefore是正確的I - 1。 – spandana 2011-05-26 10:30:03

+0

我會照你告訴我那樣做。謝謝。 – spandana 2011-05-26 10:30:36

+1

它給我像NSMutable陣列警告不會迴應-insertObject – spandana 2011-05-26 10:34:21

0

試試這個。

[array addObject:object];