2011-04-20 72 views
1

我在將一個自定義類和UIImage視圖混合在一個數組中時遇到了一些問題。這些都存儲在陣列中,我正在使用:對象類型更改

if ([[fixtures objectAtIndex:index] isKindOfClass:[Fixture class]]) 

以區分它是否是UIIMage或Fixture對象。爲此我的源代碼是:

- (void) moveActionGestureRecognizerStateChanged: (UIGestureRecognizer *) recognizer 
    { 
    switch (recognizer.state) 
     { 
     case UIGestureRecognizerStateBegan: 
      { 
       NSUInteger index = [fixtureGrid indexForItemAtPoint: [recognizer locationInView: fixtureGrid]]; 
       emptyCellIndex = index; // we'll put an empty cell here now 

       // find the cell at the current point and copy it into our main view, applying some transforms 
       AQGridViewCell * sourceCell = [fixtureGrid cellForItemAtIndex: index]; 
       CGRect frame = [self.view convertRect: sourceCell.frame fromView: fixtureGrid]; 
       dragCell = [[FixtureCell alloc] initWithFrame: frame reuseIdentifier: @""]; 

       if ([[fixtures objectAtIndex:index] isKindOfClass:[Fixture class]]) { 
        Fixture *newFixture = [[Fixture alloc] init]; 
        newFixture = [fixtures objectAtIndex:index]; 
        dragCell.icon = [UIImage imageNamed:newFixture.fixtureStringPath]; 
        [newFixture release]; 
       } else { 
        dragCell.icon = [fixtures objectAtIndex: index]; 
       } 
       [self.view addSubview: dragCell]; 
    } 
} 

然而,拖着那類燈具的目標單元格時,我會得到錯誤,如EXC_BAD_ACCESS或無法識別的選擇發送到實例(這是有道理的,因爲它是發送。CALayerArray規模命令

因此,我設置一個斷點,看燈具陣列內這裏我看到UIImages都設置爲正確的類類型,但也有:

  • (CALayerArray *)
  • (夾具*)
  • (NSObject的*)

的位置是正在陣列中保持的夾具類。任何人都可以闡明它爲什麼這樣做嗎?如果您需要更多信息,請隨時提問。

丹尼斯

回答

4

在你的代碼在這裏:

Fixture *newFixture = [[Fixture alloc] init]; 
newFixture = [fixtures objectAtIndex:index]; 
dragCell.icon = [UIImage imageNamed:newFixture.fixtureStringPath]; 
[newFixture release]; 

它看起來像你釋放一個自動釋放對象(newFixture)。當你從數組中獲得一個對象時,它就是autorelease。 你也有內存泄漏,當你在第一行分配newFixture時,這個對象永遠不會被釋放,因爲你將第二行中的指針替換爲它。

Fixture *newFixture = [[Fixture alloc] init]; // THIS OBJECT IS NEVER RELEASED 
newFixture = [fixtures objectAtIndex:index]; // YOU'RE REPLACING THE newFixture POINTER WITH AN OBJECT FROM THE ARRAY 
dragCell.icon = [UIImage imageNamed:newFixture.fixtureStringPath]; 
[newFixture release]; // YOU'RE RELEASING AN AUTORELEASED OBJECT 

因此,代碼應該像

Fixture *newFixture = [fixtures objectAtIndex:index]; 
dragCell.icon = [UIImage imageNamed:newFixture.fixtureStringPath]; 

那麼你的屬性應該保持正確的圖像。