2011-05-24 88 views
0

下面的代碼運行沒有任何錯誤,但沒有給我確切的結果。NSMutableDictionary-> Objective C

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

在上述即時刪除對應於該鍵的值的代碼(wTimerId = 95),但 雖然密鑰現有它不是漸入如果環路和刪除相應的 鍵值對。

回答

1

您正在檢查相同的地址[NSNumber numberWithUnsignedShort:wTimerIds] == key而不是同一個對象。嘗試

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

@Deepak:Thanks.But我得到一個運行時異常。2011-05-24 19:35:26.987 NSArray [7755:a0f]鎖定 ]進入while循環! ]沒有這個ID的鑰匙 ]進入while循環! ]沒有這個ID的鑰匙 ]進入while循環! ]沒有這個ID的鑰匙 ]進入while循環! ***取消密鑰 ]刪除密鑰 ***由於未捕獲的異常'NSGenericException',原因:'*** Collection 在終止應用程序時被枚舉。 – spandana 2011-05-24 14:02:03

+0

這是因爲你試圖從一個快速枚舉的對象中移除。難道你不能說'[m_cAppIdMap removeObjectForKey:[NSNumber numberWithUnsignedShort:wTimerIds]];'並刪除整個循環。這將刪除元素,如果它存在。 – 2011-05-24 14:06:21

+0

@Deepak:是的,我有元素,我要刪除的元素是與wtimerId 95.那麼爲什麼它會拋出一個運行時異常。它進入if循環,並刪除它後,它扔我例外。 – spandana 2011-05-24 14:13:08

1

替換所有這一切:

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的整點是,你不通過按鍵需要每次循環。

注意Deepak關於爲什麼你的if語句不起作用的答案對於這個狹窄問題是正確的,但他忽略了更廣泛的問題。

當你在它的時候,你應該修復內存泄漏。你是malloc pEvent,但只有在找到密鑰時纔會釋放它。

+0

感謝您向我解釋我錯在哪裏..是否可以在枚舉期間將值插入到地圖中。爲什麼他們在字典中使用枚舉。 – spandana 2011-05-24 14:39:07

+1

在枚舉期間,您無法以任何方式更改集合。這包括插入和刪除。您通常會列舉字典,如果您想對其中的每個鍵/值對執行某些操作,例如如果你想打印它的內容或將其串行化。 – JeremyP 2011-05-24 15:08:12