[OGo-Developer] stuffing an NSArray into a NSMutableDictionary

Sebastian Reitenbach developer@opengroupware.org
Fri, 20 Apr 2007 07:03:33 +0200


Hi Rafel,

> 
> I think the followin code must work:
> 
> <your code example, thanks a lot for that>
> 
> In fact, the final value of self->multipleReturn is an array with only 
> one element,
> a dictionary. If you need that self->multipleReturn must be an array, I 
> think that's ok,
> but it also could be a dictionary or a variable of type id initially 
> containing an array
> and finally containing a dictionary.

I too got it working for one queue by myself, I (hope) mentioned some mails 
ago, there can be an arbitrary number of queues (QueueParams events) where 
an arbitrary number of agents is assigned to each queue. So I took your 
suggestion and tried to enhance it to work for multiple queues:


- (NSArray *)getQueues {
  NSException *exc;
  if ((exc = [self sendCommand:@"QueueStatus"
                withParameters:nil
                expectResult:[[[self asteriskCommands] 
objectForKey:@"QueueStatus"] objectForKey:@"ExpectedResult"]])) {
    ASSIGN(self->lastException, exc);
    return nil;
  }
  else {
    /* need to sort the output in multiple */
NSLog(@"self->multipleReturn after the sendCommand: %@", 
self->multipleReturn);
    NSEnumerator *e;
    NSMutableDictionary *queuesdict;
    NSMutableArray *tmparray;
    id event;

    queuesdict = [[NSMutableDictionary alloc] init];
    tmparray = [[NSMutableArray alloc] init];

    e = [self->multipleReturn objectEnumerator];
    while ((event = [e nextObject]) != nil) {
      if ([[event objectForKey:@"Event"] hasSuffix:@" QueueParams"]) {
        /* the queue description, their parameters */
        [queuesdict takeValue:event forKey:[event objectForKey:@"Queue"]];
      } else {
        /* queue members */
        [tmparray release];
        if ([[queuesdict objectForKey:[event  objectForKey:@"Queue"]] 
                                         objectForKey:@"Agents"] != nil )
          tmparray = [NSMutableArray arrayWithObject:[[queuesdict 
                         objectForKey:[event  objectForKey:@"Queue"]] 
                         objectForKey:@"Agents"]];
        else
          tmparray = [[NSMutableArray alloc] initWithCapacity:8];

          [tmparray addObject:event];
          [[queuesdict objectForKey:[event  objectForKey:@"Queue"]]   
             takeValue:[[NSMutableArray arrayWithObject:tmparray] retain] 
             forKey:@"Agents"];
      }
    }
    [self->multipleReturn removeAllObjects];
    NSEnumerator *f;
    id aqueue;

    f = [queuesdict objectEnumerator];
    while ((aqueue = [f nextObject]))
      [self->multipleReturn addObject:aqueue];
  }
    return self->multipleReturn;
}

at least I got that far, so that [tmparray addObject:event] in the else part 
works and fills the tmparray.

but the next line:

          [[queuesdict objectForKey:[event  objectForKey:@"Queue"]]   
             takeValue:[[NSMutableArray arrayWithObject:tmparray] retain] 
             forKey:@"Agents"];

is still not working as I thought it would. After this statement, the 
queuesdict still only contains the QueueParams Event, but not the agents.


kind regards
Sebastian