Objective - C调用变量不起作用

时间:2015-04-27 20:57:03

标签: objective-c memory-management shinobi

我在这里定义了这个变量:

const SDataGridCoord *clickedGridCoord;

我在这个方法中填充它:

- (void)shinobiDataGrid:(ShinobiDataGrid *)grid willSelectCellAtCoordinate:(const SDataGridCoord *)gridCoordinate
{
    if ((gridCoordinate.column.displayIndex==5||gridCoordinate.column.displayIndex == 6 || gridCoordinate.column.displayIndex == 7 ) && dataSource.dataForDatabase)
    {
        clickedGridCoord  = gridCoordinate;
        [self CreateCellDateModPopup:gridCoordinate];
    }
}

我使用了一个断点并看到它被填充。

但是当我调用它时,它是空的:

CellData *cell = [dataSource.cellHolder objectAtIndex:clickedGridCoord.row];

并且为空我的意思是没有行或列,但是当我第一次填充SDataGridCoord时。

我做错了什么,没有别的东西可以覆盖变量。

我试过这个:

SDataGridCoord *clickedGridCoord;

然后

- (void)shinobiDataGrid:(ShinobiDataGrid *)grid willSelectCellAtCoordinate:(const SDataGridCoord *)gridCoordinate
{
    if ((gridCoordinate.column.displayIndex==5||gridCoordinate.column.displayIndex == 6 || gridCoordinate.column.displayIndex == 7 ) && dataSource.dataForDatabase)
    {
        clickedGridCoord = (SDataGridCoord *)gridCoordinate;
        [self CreateCellDateModPopup:gridCoordinate];
    }
}

仍然是相同的结果,我的应用程序仍然崩溃:线程1:EXC_BAD_ACCESS(代码= 1,地址= 0xc000000c)

我的.m文件:

@interface Controller()
{
     SDataGridCoord *clickedGridCoord;
}

@implementation Controller

- (void)shinobiDataGrid:(ShinobiDataGrid *)grid willSelectCellAtCoordinate:(const SDataGridCoord *)gridCoordinate
{
    if ((gridCoordinate.column.displayIndex==5||gridCoordinate.column.displayIndex == 6 || gridCoordinate.column.displayIndex == 7 ) && dataSource.dataForDatabase)
    {
        clickedGridCoord = (SDataGridCoord *)gridCoordinate;
        [self CreateCellDateModPopup:clickedGridCoord];
    }
}


- (void)ChangeCellWithStringDate :(NSString *)stringDate
{
    //My app crashes here with this error: Thread 1:EXC_BAD_ACCESS (code = 1, address=0xc000000c)
    CellData *cell = [dataSource.cellHolder objectAtIndex:clickedGridCoord.row.rowIndex];
}

2 个答案:

答案 0 :(得分:2)

您使用的是一些预ARC库吗?如果是这样,我怀疑某人(以及某人的意思是某些代码)会再次手动释放 gridCoordinate 引用的对象。

这会导致对象被释放,并最终导致对错误的内存位置的引用(因为对象已经消失)。

如果你有SDataGridCoord的源代码,请在dealloc方法中添加一个断点,看看是否在你尝试使用

中的对象之前调用它
- (void)ChangeCellWithStringDate :(NSString *)stringDate

最后,如果发生这种情况,您应该创建另一个SDataGridCoord对象(只有您自己的副本)并将其分配给您的ivar。它将由ARC管理,一切都会好的。

答案 1 :(得分:0)

尝试将您的变量添加到.m

的界面
@interface YOUR_OBJECT ()
{
    SDataGridCoord *_clickedGridCoord;
}
@end