2012-04-03 27 views
0

複製對象的正確方法,所以我想從一個NSMutableArray複製對象:什麼是從一個NSMutableArray

self.myObject = [self.myMutableArray objectAtIndex:self.currentIndex]; 

但當currentIndex變化,myObject變化對應於新的索引的對象。

解決此問題的最佳方法是什麼?

編輯:

這裏或多或少我想要實現:

self.currentIndex = 0; 

self.myObject = [self.myMutableArray objectAtIndex:self.currentIndex]; 

self.currentIndex = 1; 

//After here I want myObject to remain the same, but it doesn't 
+0

沒有足夠的信息來正確回答這個問題:如果你不知道對象的索引,你需要知道somet除此之外,還可以讓你唯一地識別你的對象。請告訴我們是什麼使得您需要從數組中獲取的對象與其他索引中的對象不同。 – dasblinkenlight 2012-04-03 12:27:25

+0

你的問題不清楚...所以你想讓你的對象改變或不改變? – skytz 2012-04-03 12:29:39

+0

對不起,我剛更新了它。 – sooper 2012-04-03 12:30:45

回答

6

調用該數組中的對象的copy方法,然後把自己釋放出來的護理:

self.myObject = [[self.myMutableArray objectAtIndex:5] copy]; 
... 
[self.myObject release]; 
+0

我現在就試試看,謝謝 – sooper 2012-04-03 12:34:34

相關問題