0
votes

At mouseDown and mouseDragged:

locll = [self convertPoint: [event locationInWindow] fromView:nil];
NSValue *locationValuell = [NSValue valueWithPoint:locll];
[vertices addObject:locationValuell];

at mouseUp

NSString *index = [NSString stringWithFormat:@"%d", aIndex++];
[aDict setObject:vertices forKey:index];
NSArray *allKeys = [aDict allKeys];
NSLog(@"dict count: %ld", [allKeys count]);
NSString *index1 = [NSString stringWithFormat:@"%d", aIndex];
NSMutableArray *a = [aDict objectForKey:index1];
NSLog(@"a count:%li", [a count]);

at initWithCoder

int aIndex = 0;

dict count returns how many objects are stored in dictionary. And it works. But later when I try to get array back from dictionary, I check how much objects array has ([a count]) and it returns 0. So i guess NSMutableDictionary empties my NSMutableArray, or I am taking it back in wrong way.

3

3 Answers

2
votes

Here

NSString *index = [NSString stringWithFormat:@"%d", aIndex++];

you increment aIndex after using it (post-increment). So if index is "0" then index1 will be "1". Therefore

NSMutableArray *a = [aDict objectForKey:index1];

returns nil.

0
votes

You are increasing your aIndex by 1 after using it for saving vertices in aDict. And you access this key after it(aIndex) is increased by 1. Naturally your aDict doesn't contain any array for that key.

0
votes

Just try out this:

       NSMutableArray *a=[NSMutableArray arrayWithObjects:[aDict objectForKey:index1], nil];

It should be work.