2012-07-30 49 views
1

我想從UITouches中存儲CGPoints到數組中。從閱讀其他線程我已經意識到使用NSValue似乎是做到這一點的最佳方式。 目前我有:CGPoints,NSValues,NSMutableArray和ID類型

NSMutableArray *dotsArray; 
NSValue *pointObj; 
UITouch *touch = obj; 
CGPoint touchPoint = [touch locationInView:self.view]; 
// Need to store the CGPoints, can't store directly into an array // as it is a C struct   // so we use NSValue 
// CGPoint converted to NSValue 
CGPoint point = touchPoint; 
pointObj = [NSValue valueWithCGPoint:point]; 
dotsArray = [NSArray arrayWithObjects:pointObj,nil]; 
// Print to console the objects in the collection 
NSLog(@"array content: %@", dotsArray); 
NSLog(@"[dotsArray count] = %i",[dotsArray count]); 
// Restore from NSValue to C structures 
CGPoint pointRestored = [pointObj CGPointValue]; 

我希望能夠使用:

[dotsArray insertObject:pointObj atIndex:0]; 

代替

dotsArray = [NSArray arrayWithObjects:pointObj,nil]; 

,這樣我可以增加配售我記得的方法存儲多個點。

從閱讀我認爲這與'insertObject'所需的ID類型有關,但我不完全理解它,並且無法在線找到解決方案。

編輯:當我使用

[dotsArray insertObject:pointObj atIndex:0]; 

然後

NSLog(@"array content: %@", dotsArray); 

輸出NULL

回答

0

使用addObject:方法。

[dotsArray addObject:pointObj]; 

我認爲這會對您有所幫助。

+0

嗨Prasad G,我已經試過這個,當我使用它,我收到了,數組內容:(空)和dotsArray計數] = 0,從NSLog在代碼中。我還需要能夠在選定的索引處添加到數組。 – 2012-07-30 11:21:36

+0

dotsArray = [NSMutableArray alloc] init];你應該在viewDidLoad方法中寫入並將dotsArray聲明爲全局變量。 – 2012-07-30 11:23:29

+0

非常感謝。我需要一套新的眼睛!我把它在錯誤的地方宣佈了。謝謝。 – 2012-07-30 11:30:18

0

要使用[dotsArray insertObject:pointObj atIndex:0];指陣列的ID,它是在陣列的位置的方法。這意味着atIndex:0是數組從第零位開始的第一個位置。所以現在如果你把它放在你的DotsArray中,你想調用的索引或ID類型就是你想要顯示的點的值。

+0

我編輯了這個問題,使我的問題更加清晰。當我使用上述方法時,它的NSLogs爲NULL。有任何想法嗎? – 2012-07-30 11:14:42