我想從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
嗨Prasad G,我已經試過這個,當我使用它,我收到了,數組內容:(空)和dotsArray計數] = 0,從NSLog在代碼中。我還需要能夠在選定的索引處添加到數組。 – 2012-07-30 11:21:36
dotsArray = [NSMutableArray alloc] init];你應該在viewDidLoad方法中寫入並將dotsArray聲明爲全局變量。 – 2012-07-30 11:23:29
非常感謝。我需要一套新的眼睛!我把它在錯誤的地方宣佈了。謝謝。 – 2012-07-30 11:30:18