I have an array of employee objects. Whenever I print them using NSLog(), the array gets logged but it starts to deallocate the employee objects though I have not assigned the objects to nil. Here is my Employee.h :
@interface : Employee:NSObject
{
NSMutableArray *assetsarray;
}
@property int ID;
-(void)dealloc;
-(NSString *)description;
Employee.m :
@synthesize ID;
-(void)dealloc
{
NSLog(@"Deallocating Employee %i" , self.ID);
}
-(NSString *)description
{
return [NSString stringWithFormat:@"Employee %i" , self.ID];
}
Here is my main.m : (Please ignore the comments)
int main(int argc, const char * argv[])
{
@autoreleasepool {
NSMutableArray *employeesarray = [NSMutableArray array];
// NSMutableArray *assetsarray = [NSMutableArray array];
for (int i = 1; i < 11; i++) {
Employee *emp = [[Employee alloc]init];
[emp setID:i];
[employeesarray addObject:emp];
}
//
// for (int j = 1; j<11; j++) {
//
//
// Asset *ass = [[Asset alloc]init];
//
// ass.label = [NSString stringWithFormat:@"Laptop %i" , j];
// ass.resaleValue = j*18;
//
// [assetsarray addObject:ass];
//
// int index = arc4random() % [employeesarray count];
//
//
// [[employeesarray objectAtIndex:index] addAssetsObject:ass];
//
//
// }
NSLog(@"%@" , employeesarray);
}
return 0;
}
Here is the output :
2014-03-18 20:45:16.456 Employee[2112:303] (
"Employee 1",
"Employee 2",
"Employee 3",
"Employee 4",
"Employee 5",
"Employee 6",
"Employee 7",
"Employee 8",
"Employee 9",
"Employee 10"
)
2014-03-18 20:45:16.458 Employee[2112:303] Deallocating Employee 1
2014-03-18 20:45:16.458 Employee[2112:303] Deallocating Employee 2
2014-03-18 20:45:16.458 Employee[2112:303] Deallocating Employee 3
2014-03-18 20:45:16.459 Employee[2112:303] Deallocating Employee 4
2014-03-18 20:45:16.459 Employee[2112:303] Deallocating Employee 5
2014-03-18 20:45:16.459 Employee[2112:303] Deallocating Employee 6
2014-03-18 20:45:16.460 Employee[2112:303] Deallocating Employee 7
2014-03-18 20:45:16.460 Employee[2112:303] Deallocating Employee 8
2014-03-18 20:45:16.460 Employee[2112:303] Deallocating Employee 9
2014-03-18 20:45:16.460 Employee[2112:303] Deallocating Employee 10
Program ended with exit code: 0