2011-05-26 77 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];