2012-07-31 57 views
0

在鼠标按下和的mouseDragged:的NSMutableDictionary返回空的NSMutableArray

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

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

在的initWithCoder

int aIndex = 0; 

字典计回报多少对象存储在字典中。它的工作原理。但后来当我尝试从字典中获取数组时,我检查数组中有多少对象([count]),它返回0.所以我猜NSMutableDictionary清空了我的NSMutableArray,或者我以错误的方式回收它。

回答

2

这里

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

使用它(后增)你增加aIndex后。所以如果index是“0”,那么index1将是“1”。因此,

NSMutableArray *a = [aDict objectForKey:index1]; 

返回nil

0

将aIndex保存为aDict后,您将使用aIndex增加1。并且在它(aIndex)增加1之后访问此键。自然,您的aDict不包含该键的任何数组。

0

刚刚尝试了这一点:

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

它应该是工作。

相关问题