2012-03-22 49 views
0

我有一個包含數組的對象。在初始化這個對象時,數組被分配並正確填充(正如我在調試器中看到的那樣)。該對象用於管理單個視圖中的元素。數組對象不保留它的數據

我的問題是,當我嘗試第二次調用該對象時,該數組(和該對象的所有其他參數)都是零,但它們有一個內存地址(同樣在調試器中看到)。

這是對象的有問題的.H:

#import <Foundation/Foundation.h> 

#import "ObjectDef.h" 
#import "AbstractNode.h" 

@interface RenderingMachine : NSObject 
{  
    NSMutableArray* _objectID; // pair list 
    NSMutableArray* _objectList; // node list 
    ObjectDef* _defs;    // definition of pairs 

    unsigned int _size; 
    unsigned int _edgeSize; 

    AbstractNode* _lastNode; 
} 

-(void) InitializeMachine; 
-(bool) AddObjectByIndex:(int)index :(float)x :(float)y :(float)originX :(float)originY; 
-(bool) AddObjectByType:(NSString*)type; 
-(NSMutableArray*) GetObjectID; 
-(NSMutableArray*) GetObjectList; 
-(unsigned int) Size; 
-(void) DrawAllNode; 
-(int) ComputePar; 
-(void) ComputeLastEdge:(int)edgeCount; 

//+(RenderingMachine*) GetMachine; 

@end 

我的主要問題,現在是填充在InitDefinitions _defs:

-(void) InitializeMachine 
{ 
    _defs = [[ObjectDef alloc] init]; 
    [_defs InitDefinitions]; 
    _objectID = [[NSMutableArray alloc] init]; 
    _objectList = [[NSMutableArray alloc] init]; 
    _objectID = [NSMutableArray arrayWithObject:[_defs GetPair:3]]; // adding the field node ID 

    AbstractNode* rootNode = [[FieldNode alloc] init]; 
    _objectList = [NSMutableArray arrayWithObject:rootNode]; // adding the field node as root node 

    _size = 1; 
    _edgeSize = 0; 
} 

我想知道是什麼是否可能是一個錯誤的alloc/init調用,或者它可能是xcode的ARC問題,因爲這個特定的文件用ARC編譯(另一個用「-fno-objc-arc」忽略)。

另外,正如所提到的_defs有問題,但在@interface下聲明的所有屬性都有相同的問題。

+0

您需要包括更多的代碼。 ObjectDef的init方法和初始化的InitDefinitions,以及您創建RenderingMachine並從中調用InitializeMachine的任何代碼。另外,請按照Objective-C慣例使用方法,伊娃和班級命名 - 只有課程以大寫字母開頭。如果你使用你的方法名稱,它會讓它閱讀起來更難! – jrturton 2012-03-22 07:35:37

+0

另外,您不需要兩次初始化可變數組,然後嘗試使用arrayWithCapacity,然後使用addObject:或僅使用arrayWithObject:單獨使用。然後,如建議,嘗試發佈更多的代碼。 – Leonardo 2012-03-22 08:41:56

回答

2

首先創建與_objectID = [[NSMutableArray alloc] init];
一個保留的對象,然後你用自動釋放一個_objectID = [NSMutableArray arrayWithObject:[_defs GetPair:3]];

覆蓋它添加的對象更好地利用[_objectID addObject:[_defs GetPair:3]];

同樣的事情與_objectList對象