开发者

storing,deleting elements in array

I am having an array(m_cPendingEventList) and a dictionary(m_cAppIdMap).when i store the values and keys onto the map its unordered and so i'm setting up a mutable array to hold the values in order.I am inserting only one element into the array(m_cPendingEventList). and the element that is stored is a pointer to the structure.

When i'm just printing the dictionary i'm getting the same value for the different keys.However i'm changing the values of one field and storing that structure object onto the map.When i print to see the contents of the map.All the values its showing me for the different keys are the same.Is it the address of the structure pointer.I am new to Objective C and programming.

The problem i'm facing with this code is that i could not able to delete the value stored on the NSMutable array(m_cAppIdMap).

I have given a key for which the value has to be deleted on the array. I will search the value for the particular key in the map and will pass that value as an argument to the function findAndRemove to delete the value inthat array.Here I could not able to delete the element in the array.

-(BOOL)createTimer
{
    stRs232Timer*   pEvent = malloc(sizeof(stRs232Timer));

    pEvent->bPersistent = YES;                              // setup timer structure
    pEvent->wAppTimerId = 95;
    pEvent->uPeriod     = 50;
    pEvent->bStopped    = NO;


    NSLog(@"bPersistent:%d",pEvent->bPersistent);
    NSLog(@"wAppTimerId:%d",pEvent->wAppTimerId);
    NSLog(@"uPeriod:%d",pEvent->uPeriod);
    NSLog(@"bStopped:%d",pEvent->bStopped);

    theLock = [[NSLock alloc]init];

    NSData* myData = [NSData dataWithBytes:&pEvent length:sizeof(pEvent)];

    wTimerId = 99;
    pEvent->uPeriod = 51;
    myDa开发者_开发百科ta = [NSData dataWithBytes:&pEvent length:sizeof(pEvent)];
    [m_cAppIdMap setObject:myData forKey:[NSNumber numberWithUnsignedShort:wTimerId]];
    wTimerId = 96;
    pEvent->uPeriod = 52;
    myData = [NSData dataWithBytes:&pEvent length:sizeof(pEvent)];
    [m_cAppIdMap setObject:myData forKey:[NSNumber numberWithUnsignedShort:wTimerId]];
    wTimerId = 97;
    pEvent->uPeriod = 53;
    myData = [NSData dataWithBytes:&pEvent length:sizeof(pEvent)];
    [m_cAppIdMap setObject:myData forKey:[NSNumber numberWithUnsignedShort:wTimerId]];
    wTimerId = 98;
    pEvent->uPeriod = 54;
    myData = [NSData dataWithBytes:&pEvent length:sizeof(pEvent)];
    [m_cAppIdMap setObject:myData forKey:[NSNumber numberWithUnsignedShort:wTimerId]];
    wTimerId = 95;
    pEvent->uPeriod = 55;
    myData = [NSData dataWithBytes:&pEvent length:sizeof(pEvent)];
    [m_cAppIdMap setObject:myData forKey:[NSNumber numberWithUnsignedShort:wTimerId]];

    NSLog(@"The dictionary count now is:%d",[m_cAppIdMap count]);
    NSLog(@"The dictionary values now is:");
    NSLog(@"%@",m_cAppIdMap);


    [m_cPendingEventList addObject:myData];
    NSLog(@"EventList:%@",m_cPendingEventList);

    [self KillTimer:95];

    int k = [m_cAppIdMap count];
    NSLog(@"The count of dict :%d",k);
    NSLog(@"My dictionary is:%@",m_cAppIdMap);
    return YES;
}
-(BOOL)KillTimer:(unsigned short)wTimerIds
{
    stRs232Timer* pEvent = malloc(sizeof(stRs232Timer));
    BOOL bReturn=NO;
    theLock = [[NSLock alloc]init];

    if ([theLock tryLock]) {

        NSLog(@"Locked");
            if ([NSNumber numberWithUnsignedShort:wTimerIds]) {
        [m_cAppIdMap removeObjectForKey:[NSNumber numberWithUnsignedShort:wTimerIds]];
        [self findAndRemoveEvent:pEvent];
             }
             NSLog(@"The Dict is:%@",m_cAppIdMap);
            //  NSLog(@"Removed the key");
             free(pEvent);
        /*  }
            else {
                NSLog(@"No key with this Id");
            }
            i++;
            bReturn = YES;
        }*/
        NSLog(@"Unlocked!!");
        NSLog(@"Into KillAll!!!");
        [theLock unlock];
        [self KillAll];
    }   

    return bReturn;
}
-(void)KillAll
{
    //unsigned short wKey;
    stRs232Timer* pEvent;

    theLock = [[NSLock alloc]init];

    /*if ([theLock tryLock]) {

        [m_cPendingEventList removeAllObjects];

        NSEnumerator* enumerator = [m_cAppIdMap keyEnumerator];
        id key;

        while((key = [enumerator nextObject]))
        {
            [m_cAppIdMap objectForKey:[NSNumber numberWithUnsignedShort:wTimerId]];
            free(pEvent);
        }
        [m_cAppIdMap removeAllObjects];
        [theLock unlock];
    }*/
    if([theLock tryLock]){
     [m_cPendingEventList removeAllObjects];
     [m_cAppIdMap removeAllObjects];
    }
    [theLock unlock];
    NSLog(@"The dict now contains:%@",m_cAppIdMap);
}
-(BOOL)findAndRemoveEvent:(const stRs232Timer*)pEvent
{
    int index;
    index = [m_cPendingEventList count];
    for(int i=0;i<index;i++)
    {
        stRs232Timer* stTimer = (stRs232Timer*)[m_cPendingEventList objectAtIndex:i];
        if(stTimer == pEvent)
        {
            NSLog(@"Found the event to remove!!");
            [m_cPendingEventList removeObjectAtIndex:i];
            NSLog(@"Event Removed!!");
        }
        else {
            NSLog(@"No such event!!");
        }

    }
    NSLog(@"The array is:%@",m_cPendingEventList);
    return YES;
}

output:

2011-05-25 14:01:19.812 NSArray[2233:a0f] bPersistent:1
2011-05-25 14:01:19.833 NSArray[2233:a0f] wAppTimerId:95
2011-05-25 14:01:19.836 NSArray[2233:a0f] uPeriod:50
2011-05-25 14:01:19.837 NSArray[2233:a0f] bStopped:0
2011-05-25 14:01:19.838 NSArray[2233:a0f] The dictionary count now is:5
2011-05-25 14:01:19.838 NSArray[2233:a0f] The dictionary values now is:
2011-05-25 14:01:19.839 NSArray[2233:a0f] {
    98 = <b0ca1000 01000000>;
    97 = <b0ca1000 01000000>;
    96 = <b0ca1000 01000000>;
    99 = <b0ca1000 01000000>;
    95 = <b0ca1000 01000000>;
}
2011-05-25 14:01:19.840 NSArray[2233:a0f] EventList:(
    <b0ca1000 01000000>
)
2011-05-25 14:01:19.845 NSArray[2233:a0f] Locked
2011-05-25 14:01:19.846 NSArray[2233:a0f] No such event!!
2011-05-25 14:01:19.846 NSArray[2233:a0f] The array is:(
    <b0ca1000 01000000>
)
2011-05-25 14:01:19.847 NSArray[2233:a0f] The Dict is:{
    98 = <b0ca1000 01000000>;
    97 = <b0ca1000 01000000>;
    96 = <b0ca1000 01000000>;
    99 = <b0ca1000 01000000>;
}
2011-05-25 14:01:19.848 NSArray[2233:a0f] Unlocked!!
2011-05-25 14:01:19.848 NSArray[2233:a0f] Into KillAll!!!
2011-05-25 14:01:19.849 NSArray[2233:a0f] The dict now contains:{
}
2011-05-25 14:01:19.849 NSArray[2233:a0f] The count of dict :0
2011-05-25 14:01:19.850 NSArray[2233:a0f] My dictionary is:{
}


pEvent is already a pointer. Why are you sending a pointer to it. You will have to create the NSData object like this –

myData = [NSData dataWithBytes:(void*)pEvent length:sizeof(pEvent)];

This should work.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜