开发者

NSMutableDictionary->Objective C [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. Closed 11 years ago.

The code below is running without any errors but is not giving me the exact result.

-(id)init
{
    if (self = [super init]) {
        m_cAppIdMap = [[NSMutableDictionary alloc]init];
        //enumerator = [m_cAppIdMap keyEnumerator];
    }
    return self;
}
-(BOOL)createTimer
{
    stRs232Timer*   pEvent = malloc(sizeof(stRs232Timer));

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

    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)];
    [m_cAppIdMap setObject:myData forKey:[NSNumber numberWithUnsignedShort:wTimerId]];
    wTimerId = 96;
    [m_cAppIdMap setObject:myData forKey:[NSNumber numberWithUnsignedShort:wTimerId]];
    wTimerId = 97;
    [m_cAppIdMap setObject:myData forKey:[NSNumber numberWithUnsignedShort:wTimerId]];
    wTimerId = 98;
    [m_cAppIdMap setObject:myData forKey:[NSNumber numberWithUnsignedShort:wTimerId]];
    wTimerId = 99;
    [m_cAppIdMap setObject:myData forKey:[NSNumber numberWithUnsignedShort:wTimerId]];


    NSLog(@"The dictionary count now is:%d",[m_cAppId开发者_StackOverflow中文版Map count]);
    NSLog(@"The dictionary values now is:");
    NSLog(@"%@",m_cAppIdMap);

    [self KillTimer:wTimerId];

    if ([theLock tryLock]) {
        //wTimerId=100;
        //[m_cAppIdMap setObject:myData forKey:[NSNumber numberWithUnsignedShort:wTimerId]];
        [theLock unlock];
    }   
    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");
        NSEnumerator* enumerator = [m_cAppIdMap keyEnumerator];
        id key;
        while((key = [enumerator nextObject]))
        {
            NSLog(@"Into the while loop!!");
            if ([NSNumber numberWithUnsignedShort:wTimerIds] == key) {
             NSLog(@"Got key to remove");
             //[self findAndRemoveEvent:pEvent];
             [m_cAppIdMap removeObjectForKey:[NSNumber numberWithUnsignedShort:wTimerIds]];
              NSLog(@"Removed the key");
             free(pEvent);
            }
            else {
                NSLog(@"No key with this Id");
            }

            bReturn = YES;
        }
        NSLog(@"Unlocked!!");
        [theLock unlock];
    }   

    return bReturn;
}

OUTPUT

2011-05-24 19:19:02.915 NSArray[7558:a0f] bPersistent:1
2011-05-24 19:19:02.918 NSArray[7558:a0f] wAppTimerId:95
2011-05-24 19:19:02.919 NSArray[7558:a0f] uPeriod:50
2011-05-24 19:19:02.919 NSArray[7558:a0f] bStopped:0
2011-05-24 19:19:02.920 NSArray[7558:a0f] The dictionary count now is:5
2011-05-24 19:19:02.920 NSArray[7558:a0f] The dictionary values now is:
2011-05-24 19:19:02.921 NSArray[7558:a0f] {
    98 = <70c71000 01000000>;
    97 = <70c71000 01000000>;
    96 = <70c71000 01000000>;
    99 = <70c71000 01000000>;
    95 = <70c71000 01000000>;
}
2011-05-24 19:19:02.921 NSArray[7558:a0f] Locked
2011-05-24 19:19:02.925 NSArray[7558:a0f] Into the while loop!!
2011-05-24 19:19:02.925 NSArray[7558:a0f] No key with this Id
2011-05-24 19:19:02.926 NSArray[7558:a0f] Into the while loop!!
2011-05-24 19:19:02.926 NSArray[7558:a0f] No key with this Id
2011-05-24 19:19:02.927 NSArray[7558:a0f] Into the while loop!!
2011-05-24 19:19:02.927 NSArray[7558:a0f] No key with this Id
2011-05-24 19:19:02.927 NSArray[7558:a0f] Into the while loop!!
2011-05-24 19:19:02.928 NSArray[7558:a0f] No key with this Id
2011-05-24 19:19:02.928 NSArray[7558:a0f] Into the while loop!!
2011-05-24 19:19:02.929 NSArray[7558:a0f] No key with this Id
2011-05-24 19:19:02.929 NSArray[7558:a0f] Unlocked!!
2011-05-24 19:19:02.930 NSArray[7558:a0f] The count of dict :5
2011-05-24 19:19:02.930 NSArray[7558:a0f] My dictionary is:{
    98 = <70c71000 01000000>;
    97 = <70c71000 01000000>;
    96 = <70c71000 01000000>;
    99 = <70c71000 01000000>;
    95 = <70c71000 01000000>;
}

In the code above i'm deleting the value corresponding to the key(wTimerId = 95).But Though the key is existing its not getting into the if loop and deleting the corresponding key value pair.


You are checking for the same address here [NSNumber numberWithUnsignedShort:wTimerIds] == key rather than the same object. Try

...
if ( [[NSNumber numberWithUnsignedShort:wTimerIds] isEqual:key] ) {
...


Replace all of this:

    NSEnumerator* enumerator = [m_cAppIdMap keyEnumerator];
    id key;
    while((key = [enumerator nextObject]))
    {
        NSLog(@"Into the while loop!!");
        if ([NSNumber numberWithUnsignedShort:wTimerIds] == key) {
         NSLog(@"Got key to remove");
         //[self findAndRemoveEvent:pEvent];
         [m_cAppIdMap removeObjectForKey:[NSNumber numberWithUnsignedShort:wTimerIds]];
          NSLog(@"Removed the key");
         free(pEvent);
        }
        else {
            NSLog(@"No key with this Id");
        }

        bReturn = YES;
    }

with this:

[m_cAppIdMap removeObjectForKey:[NSNumber numberWithUnsignedShort:wTimerIds]];

The whole point of an NSDictionary is that you do not need to loop through the keys each time.

NB Deepak's answer as to why your if statement doesn't work is the correct one for that narrow question but he ignores the wider problem.

While you're at it, you should fix the memory leak. You malloc pEvent but only free it if you find the key.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜