NSMutableDictionary->Objective C

发布于 2024-11-09 10:26:36 字数 4564 浏览 0 评论 0原文

下面的代码运行没有任何错误,但没有给我确切的结果。

-(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_cAppIdMap 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;
}

输出

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>;
}

在上面的代码中,我删除了与键对应的值(wTimerId = 95)。但是 虽然密钥存在,但它没有进入 if 循环并删除相应的 键值对。

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_cAppIdMap 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

能怎样 2024-11-16 10:26:36

您在此处检查相同的地址 [NSNumber numberWithUnsignedShort:wTimerIds] == key 而不是相同的对象。尝试

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

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

...
if ( [[NSNumber numberWithUnsignedShort:wTimerIds] isEqual:key] ) {
...
听不够的曲调 2024-11-16 10:26:36

将所有这些: 替换

    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;
    }

为:

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

NSDictionary 的全部要点是您不需要每次都循环遍历键。

NB Deepak 关于为什么你的 if 语句不起作用的答案对于这个狭隘的问题来说是正确的,但他忽略了更广泛的问题。

当你这样做时,你应该修复内存泄漏。您 malloc pEvent 但只有在找到密钥时才释放它。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文