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.
You are checking for the same address here
[NSNumber numberWithUnsignedShort:wTimerIds] == keyrather than the same object. Try