6

this is my data strucure:

group [1...n] {
  id,
  name,
  elements : [1...n]
}

I define a class for element with all properties and a class for group as:

@interface Group : NSObject {    
    NSInteger groupID;
    NSString *groupName;        
    NSMutableArray *elements;       
}

@property (assign, readwrite) NSInteger groupID;
@property (assign, readwrite) NSString *groupName;
@property (assign, readwrite) NSMutableArray *elements;

and single element with:

@interface Element : NSObject {
    NSInteger elementID;
    NSString *elementName;
}
@property (assign, readwrite) NSInteger elementID;
@property (assign, readwrite) NSString *elementName;

Both classes have properties and synthesize. When application start I inserted data on data structure with this:

arrGroup = [NSMutableArray array];
[arrGroup retain];
Element *element1 = [[Element alloc] init];
element1.elemenID = 1;
element1.elemenName = @"Andrea";

Element *element = [[Element alloc] init];
element2.elementID = 2;
element2.elementName = @"Andrea2";

Group *group = [[Group alloc] init];    
group.groupID = 1;
group.groupName = @"Grup 1";    
[group.elements addObject:element1];
[group.elements addObject:element2];

[contact1 release];
[contact2 release];

[arrGroup addObject:group];

The problem is this the [group.elements addObjct:element1]. Nothing has been written on elements NSMutableArray.

Could you help me to find the error? There is a better method to retrieve structure data (groups of elemens)?

thanks for help! Andrea

2
  • 1
    Could you add the property declarations and the code that initializes elements? Commented Jul 12, 2010 at 19:08
  • At this point I suppose you found the problem! I actually initialize the elements but I just added a NSMutableArray *elements; to group object definition. I thought that @synthesize is enough. I don't know how to initialize the elements Mutable Array of objects elements Commented Jul 12, 2010 at 19:48

1 Answer 1

15

@synthesize only generates the getter and the setter for your property, you have to take care of initialization yourself if needed.

To initialize the mutable array do e.g. this in your initializer:

- (id)init { // or however it is named
    if ((self = [super init])) {
        elements = [[NSMutableArray alloc] init];
        // ... more?
    }
    return self;
}

- (void)dealloc {
    [elements release]; // don't forget to clean up
    // ... more?
    [super dealloc];
}
Sign up to request clarification or add additional context in comments.

1 Comment

Great it works!!!!!! Thanks!!! (I wrongly defined the init, for this reason I didn't include... I've to return to the books.... :( )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.