PDA

View Full Version : Appending to arrays in Objects




calistra
Jan 12, 2011, 05:49 PM
I have an object HomeControlGroup which contains a Mutable Array of HomeControlAppliances


@class HomeControlAppliance;

@interface HomeControlGroup : NSObject {
NSString *groupName;
NSInteger groupNumber;
NSMutableArray *homeControlAppliances;
}
@property (nonatomic,retain) NSString *groupName;
@property (nonatomic,assign) NSInteger groupNumber;
@property (nonatomic,retain) NSMutableArray *homeControlAppliances;

- (id) initGroup: (NSString *) newName groupNumber: (NSInteger)newNumber;
- (void) addHomeControlAppliance: (HomeControlAppliance *) newAppliance;

@end

I can create an array of Groups initialised thus:

- (id) initGroup: (NSString *) newName groupNumber: (NSInteger)newNumber {
self = [super init];
if (self != nil) {
self.groupName = newName;
self.groupNumber = newNumber;
[self.homeControlAppliances initWithCapacity: 10];
}
return self;
}


but can neither add HomeControlAppliances using the method

- (void) addHomeControlAppliance: (HomeControlAppliance *) newAppliance {
[self.homeControlAppliances addObject: newAppliance];
}


nor by simply adding an existing array using addObjectsFromArray

Any idea what is wrong? There are no compiler errors or warnings, no console messages...

addObject just destroys the object that I want to add and addObjectsFromArray kills the array.



calistra
Jan 12, 2011, 05:56 PM
- (id) initGroup: (NSString *) newName groupNumber: (NSInteger)newNumber {
self = [super init];
if (self != nil) {
self.groupName = newName;
self.groupNumber = newNumber;
self.homeControlAppliances = [[NSMutableArray alloc] initWithCapacity: 10];
}
return self;
}