存储,删除数组中的元素

时间:2011-05-25 08:46:47

标签: objective-c

我有一个数组(m_​​cPendingEventList)和一个字典(m_cAppIdMap)。当我将值和键存储到地图上时,它是无序的,因此我设置了一个可变数组来按顺序保存值。我正在插入数组中只有一个元素(m_cPendingEventList)。并且存储的元素是指向结构的指针。

当我刚刚打印字典时,我得到了不同键的相同值。但是我正在更改一个字段的值并将该结构对象存储到地图上。当我打印以查看内容时map.All它显示我的不同键的值是相同的。它是结构指针的地址。我是Objective C和编程的新手。

我遇到这个代码的问题是我无法删除存储在NSMutable数组(m_​​cAppIdMap)上的值。

我已经给出了一个必须在数组上删除该值的键。 我将搜索地图中特定键的值,并将该值作为参数传递给函数findAndRemove,以删除数组中的值。这是我无法删除数组中的元素。

-(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;
    myData = [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;
}

输出:

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:{
}

1 个答案:

答案 0 :(得分:1)

pEvent已经是一个指针。你为什么要发一个指向它的指针。您必须像这样创建NSData对象 -

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

这应该有用。