微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

循环到NSMutableArray

如何解决循环到NSMutableArray

| 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
的值,但它将计数值显示为\'0 \'。     

解决方法

字典键必须是一个对象。 尝试用以下方法替换setObject行:
[m_cAppIdMap setObject:pEvent forKey:[NSNumber numberWithUnsignedShort:wTimerId]];
    ,首先,不要使用“ 7”前缀来命名类,因为这仅保留给Apple使用。 其次,您确定要将内容保留在结构中而不是自定义类或字典中吗?当然,结构更轻巧,但是将其集成到面向对象的程序中可能会有些麻烦。     

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。