2011-04-20 68 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]; 

那么你的属性应该保持正确的图像。