2012-07-31 53 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]; 

它應該是工作。

相關問題