цикл в NSMutableArray - PullRequest
       1

цикл в NSMutableArray

0 голосов
/ 24 мая 2011

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».

Ответы [ 2 ]

2 голосов
/ 24 мая 2011

Ключ словаря должен быть объектом.

Попробуйте заменить строку setObject следующим:

[m_cAppIdMap setObject:pEvent forKey:[NSNumber numberWithUnsignedShort:wTimerId]];
1 голос
/ 24 мая 2011

Прежде всего, не называйте свои классы, используя префикс NS, так как это зарезервировано только для Apple.

Во-вторых, вы уверены, что хотите сохранить вещи в структуре вместо пользовательского класса или словаря? Конечно, структура более легкая, но может немного затруднить интеграцию в объектно-ориентированную программу.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...