我有一个包含数组的对象。在初始化此对象时,将分配并正确填充数组(正如我在调试器中看到的那样)。此对象用于管理单个视图中的元素。
我的问题是,当我尝试第二次调用该对象时,数组(以及该对象的所有其他参数)都是nil但它们有一个内存地址(再次在调试器中看到)。
这是相关对象的.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
我现在的主要问题是使用_defs填充InitDefinitions:
-(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-忽略”)弧“)。
另外,正如提到的那样,_def是有问题的,但在@interface下声明的所有属性都有同样的问题。
答案 0 :(得分:2)
首先使用_objectID = [[NSMutableArray alloc] init];
创建保留对象
然后用自动释放的_objectID = [NSMutableArray arrayWithObject:[_defs GetPair:3]];
添加对象更好地使用[_objectID addObject:[_defs GetPair:3]];
与_objectList对象
相同