开发者

inserting onto the array

开发者 https://www.devze.com 2023-03-08 07:27 出处:网络
-(void)insertEvent:(stRs232Timer*)pEvent { BOOL bFound = NO; NSLog(@\"insertEvent\"); pEvent->uExpirationTime = pEvent->uPeriod-45;
-(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_cPendingEventL开发者_运维百科ist insertObject:(void*)pEvent atIndex:(i-1)];
                bFound = YES;
                break;
            }
        }
        i++;
    }
    if (!bFound) {
        [m_cPendingEventList insertObject:(void*)pEvent atIndex:(no+1)];//Insert last
    }
}

Is this the correct way to search the and insert the events in a correct order?

I am getting a run-time break in the if() stmts above.


Why not just use [array addObject:obj];

You don't need to specify an index - it'll insert at the end of the array.


try this.

[array addObject:object];
0

精彩评论

暂无评论...
验证码 换一张
取 消