插入到数组中

问题描述

|
-(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中遇到了运行时中断。     

解决方法

为什么不只使用
[array addObject:obj];
您不需要指定索引-它将插入数组的末尾。     ,尝试这个。
[array addObject:object];