2011-05-25 209 views
0

我有一个数组(m_​​cPendingEventList)和一个字典(m_cAppIdMap)。当我将值和键存储到其无序的地图上,所以我设置了一个可变数组来保存值我只是将一个元素插入数组(m_​​cPendingEventList)。并且存储的元素是指向结构的指针。存储,删除数组中的元素

当我只是打印词典时,我得到了不同键的相同值。但是,我正在更改一个字段的值并将该结构对象存储到地图上。当我打印查看内容时。它显示我为不同的键的所有值是相同的。它是结构指针的地址。我是Objective C和编程的新手。

我这个代码面临的问题是,我无法删除存储在NSMutable数组(m_​​cAppIdMap)上的值。

我已经给出了一个键值,该值必须在数组上删除。 我将搜索映射中特定键的值,并将该值作为参数传递给函数findAndRemove以删除数组中的值。在这里我无法删除数组中的元素。

-(BOOL)createTimer 
{ 
    stRs232Timer* pEvent = malloc(sizeof(stRs232Timer)); 

    pEvent->bPersistent = YES;        // setup timer structure 
    pEvent->wAppTimerId = 95; 
    pEvent->uPeriod  = 50; 
    pEvent->bStopped = NO; 


    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)]; 

    wTimerId = 99; 
    pEvent->uPeriod = 51; 
    myData = [NSData dataWithBytes:&pEvent length:sizeof(pEvent)]; 
    [m_cAppIdMap setObject:myData forKey:[NSNumber numberWithUnsignedShort:wTimerId]]; 
    wTimerId = 96; 
    pEvent->uPeriod = 52; 
    myData = [NSData dataWithBytes:&pEvent length:sizeof(pEvent)]; 
    [m_cAppIdMap setObject:myData forKey:[NSNumber numberWithUnsignedShort:wTimerId]]; 
    wTimerId = 97; 
    pEvent->uPeriod = 53; 
    myData = [NSData dataWithBytes:&pEvent length:sizeof(pEvent)]; 
    [m_cAppIdMap setObject:myData forKey:[NSNumber numberWithUnsignedShort:wTimerId]]; 
    wTimerId = 98; 
    pEvent->uPeriod = 54; 
    myData = [NSData dataWithBytes:&pEvent length:sizeof(pEvent)]; 
    [m_cAppIdMap setObject:myData forKey:[NSNumber numberWithUnsignedShort:wTimerId]]; 
    wTimerId = 95; 
    pEvent->uPeriod = 55; 
    myData = [NSData dataWithBytes:&pEvent length:sizeof(pEvent)]; 
    [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); 


    [m_cPendingEventList addObject:myData]; 
    NSLog(@"EventList:%@",m_cPendingEventList); 

    [self KillTimer:95]; 

    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"); 
      if ([NSNumber numberWithUnsignedShort:wTimerIds]) { 
     [m_cAppIdMap removeObjectForKey:[NSNumber numberWithUnsignedShort:wTimerIds]]; 
     [self findAndRemoveEvent:pEvent]; 
      } 
      NSLog(@"The Dict is:%@",m_cAppIdMap); 
      // NSLog(@"Removed the key"); 
      free(pEvent); 
     /* } 
      else { 
       NSLog(@"No key with this Id"); 
      } 
      i++; 
      bReturn = YES; 
     }*/ 
     NSLog(@"Unlocked!!"); 
     NSLog(@"Into KillAll!!!"); 
     [theLock unlock]; 
     [self KillAll]; 
    } 

    return bReturn; 
} 
-(void)KillAll 
{ 
    //unsigned short wKey; 
    stRs232Timer* pEvent; 

    theLock = [[NSLock alloc]init]; 

    /*if ([theLock tryLock]) { 

     [m_cPendingEventList removeAllObjects]; 

     NSEnumerator* enumerator = [m_cAppIdMap keyEnumerator]; 
     id key; 

     while((key = [enumerator nextObject])) 
     { 
      [m_cAppIdMap objectForKey:[NSNumber numberWithUnsignedShort:wTimerId]]; 
      free(pEvent); 
     } 
     [m_cAppIdMap removeAllObjects]; 
     [theLock unlock]; 
    }*/ 
    if([theLock tryLock]){ 
    [m_cPendingEventList removeAllObjects]; 
    [m_cAppIdMap removeAllObjects]; 
    } 
    [theLock unlock]; 
    NSLog(@"The dict now contains:%@",m_cAppIdMap); 
} 
-(BOOL)findAndRemoveEvent:(const stRs232Timer*)pEvent 
{ 
    int index; 
    index = [m_cPendingEventList count]; 
    for(int i=0;i<index;i++) 
    { 
     stRs232Timer* stTimer = (stRs232Timer*)[m_cPendingEventList objectAtIndex:i]; 
     if(stTimer == pEvent) 
     { 
      NSLog(@"Found the event to remove!!"); 
      [m_cPendingEventList removeObjectAtIndex:i]; 
      NSLog(@"Event Removed!!"); 
     } 
     else { 
      NSLog(@"No such event!!"); 
     } 

    } 
    NSLog(@"The array is:%@",m_cPendingEventList); 
    return YES; 
} 

输出:

2011-05-25 14:01:19.812 NSArray[2233:a0f] bPersistent:1 
2011-05-25 14:01:19.833 NSArray[2233:a0f] wAppTimerId:95 
2011-05-25 14:01:19.836 NSArray[2233:a0f] uPeriod:50 
2011-05-25 14:01:19.837 NSArray[2233:a0f] bStopped:0 
2011-05-25 14:01:19.838 NSArray[2233:a0f] The dictionary count now is:5 
2011-05-25 14:01:19.838 NSArray[2233:a0f] The dictionary values now is: 
2011-05-25 14:01:19.839 NSArray[2233:a0f] { 
    98 = <b0ca1000 01000000>; 
    97 = <b0ca1000 01000000>; 
    96 = <b0ca1000 01000000>; 
    99 = <b0ca1000 01000000>; 
    95 = <b0ca1000 01000000>; 
} 
2011-05-25 14:01:19.840 NSArray[2233:a0f] EventList:(
    <b0ca1000 01000000> 
) 
2011-05-25 14:01:19.845 NSArray[2233:a0f] Locked 
2011-05-25 14:01:19.846 NSArray[2233:a0f] No such event!! 
2011-05-25 14:01:19.846 NSArray[2233:a0f] The array is:(
    <b0ca1000 01000000> 
) 
2011-05-25 14:01:19.847 NSArray[2233:a0f] The Dict is:{ 
    98 = <b0ca1000 01000000>; 
    97 = <b0ca1000 01000000>; 
    96 = <b0ca1000 01000000>; 
    99 = <b0ca1000 01000000>; 
} 
2011-05-25 14:01:19.848 NSArray[2233:a0f] Unlocked!! 
2011-05-25 14:01:19.848 NSArray[2233:a0f] Into KillAll!!! 
2011-05-25 14:01:19.849 NSArray[2233:a0f] The dict now contains:{ 
} 
2011-05-25 14:01:19.849 NSArray[2233:a0f] The count of dict :0 
2011-05-25 14:01:19.850 NSArray[2233:a0f] My dictionary is:{ 
} 

回答

1

pEvent已经是指针。你为什么要发送一个指针。你将不得不像这样创建NSData对象 -

myData = [NSData dataWithBytes:(void*)pEvent length:sizeof(pEvent)]; 

这应该有效。

+0

@Deepak:谢谢老兄。它的工作。我现在得到的唯一keys.But不同的地址,但我无法得到this.Now还你typecasting(void *)和传递指针不是它。有什么区别它make.But这样做,我得到了答案,但我不明白它是如何发生的。还有,当我尝试通过从地图传递一个特定的键值到数组来删除它。它不会被删除。它打印我数组与已存储的值。 – spandana 2011-05-25 09:08:47

+1

'sizeof(pEvent)'? – kennytm 2011-05-25 09:14:35

+0

关于指针位。 '&pEvent'和'pEvent'都是指针,但指向什么。前者是指向存储在“pEvent”中的值的指针,后者是指向对象“pEvent”指向的指针。我们希望关于事件的信息被复制,而不是关于指针,所以你必须通过'pEvent'来代替'&pEvent'。 'void *'不会做任何魔术,它只是为了避免我猜的警告。 – 2011-05-25 09:17:23