Appending to arrays in Objects

Discussion in 'iPhone/iPad Programming' started by calistra, Jan 12, 2011.

  1. macrumors newbie

    Joined:
    Dec 14, 2010
    Location:
    Singapore
    #1
    I have an object HomeControlGroup which contains a Mutable Array of HomeControlAppliances

    Code:
    @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:
    Code:
    - (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
    Code:
    - (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.
     
  2. thread starter macrumors newbie

    Joined:
    Dec 14, 2010
    Location:
    Singapore
    #2
    OK - I am an idiot !!!!

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

Share This Page