下面的代碼運行沒有任何錯誤,但沒有給我確切的結果。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),但 雖然密鑰現有它不是漸入如果環路和刪除相應的 鍵值對。
@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
這是因爲你試圖從一個快速枚舉的對象中移除。難道你不能說'[m_cAppIdMap removeObjectForKey:[NSNumber numberWithUnsignedShort:wTimerIds]];'並刪除整個循環。這將刪除元素,如果它存在。 – 2011-05-24 14:06:21
@Deepak:是的,我有元素,我要刪除的元素是與wtimerId 95.那麼爲什麼它會拋出一個運行時異常。它進入if循環,並刪除它後,它扔我例外。 – spandana 2011-05-24 14:13:08