NSArray1.h
#import <Foundation/Foundation.h>
typedef struct
{
unsigned short wAppTimerId;
BOOL bPersistent;
uint8_t uPeriod;
BOOL bStopped;
uint8_t uExpirationTime;
} stRs232Timer;
@interface NSArray1 : NSObject {
unsigned short wTimerId;
NSLock* theLock;
NSMutableDictionary* m_cAppIdMap;
NSMutableArray* m_cPendingEventList;
}
-(BOOL)createTimer;
-(BOOL)KillTimer:(unsigned short)wTimerId;
@end
NSArray1.m
#import "NSArray1.h"
@implementation NSArray1
-(BOOL)createTimer
{
stRs232Timer* pEvent = malloc(sizeof(stRs232Timer));
pEvent->bPersistent = YES;
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];
[self KillTimer:wTimerId];
if ([theLock tryLock]) {
//CFDictionarySetValue(m_cAppIdMap,&wTimerId,pEvent);
[m_cAppIdMap setObject:pEvent forKey:wTimerId];
[theLock unlock];
}
return YES;
}
-(BOOL)KillTimer:(unsigned short)wTimerId
{
stRs232Timer* pEvent;
BOOL bReturn=NO;
theLock = [[NSLock alloc]init];
if ([theLock tryLock]) {
// remove from app map
NSLog(@"Locked");
[m_cAppIdMap setObject:pEvent forKey:wTimerId]; //warning:passing argument 1 of setObject from incompatible pointer type
int no = [m_cAppIdMap count];
NSLog(@"The no of entries in array is:%d",no);
for(id i in m_cAppIdMap)
{
NSLog(@"Going to remove a key!!");
[self findAndRemoveEvent:pEvent];
[m_cAppIdMap removeObjectForKey:wTimerId]; //warning:Local declaration of wTimerId hides instance object
NSLog(@"Removed the key"); //warning:passing argument 1 of 'objectForKey' makes pointer from integer without a cast
free(pEvent);
bReturn = YES;
}
NSLog(@"Unlocked!!");
[theLock unlock];
}
return bReturn;
}
@end
输出:
2011-05-24 14:12:55.289 NSArray[3313:a0f] bPersistent:1
2011-05-24 14:12:55.291 NSArray[3313:a0f] wAppTimerId:95
2011-05-24 14:12:55.292 NSArray[3313:a0f] uPeriod:50
2011-05-24 14:12:55.292 NSArray[3313:a0f] bStopped:0
2011-05-24 14:12:55.293 NSArray[3313:a0f] Locked
2011-05-24 14:12:55.293 NSArray[3313:a0f] The no of entries in array is:0
2011-05-24 14:12:55.294 NSArray[3313:a0f] Unlocked!!
它的not going into the for loop
。我需要找到特定键的pEvent(95)。虽然我正在设置一个值的字典并键入not giving
我的correct count
value。它将计数值显示为“0”。
答案 0 :(得分:2)
字典键必须是对象。
尝试用以下代码替换setObject行:
[m_cAppIdMap setObject:pEvent forKey:[NSNumber numberWithUnsignedShort:wTimerId]];
答案 1 :(得分:1)
首先,不要使用NS
前缀命名您的课程,因为这仅限Apple。
其次,您确定要将结构保留在结构中而不是自定义类或字典中吗?结构更轻巧,更确定,但可能会导致一些痛苦,无法集成到面向对象的程序中。