插入阵列

时间:2011-05-26 09:58:54

标签: objective-c

-(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 个答案:

答案 0 :(得分:2)

为什么不使用[array addObject:obj];

您不需要指定索引 - 它将插入数组的末尾。

答案 1 :(得分:0)

试试这个。

[array addObject:object];